New features in PHP 5.3

There is no doubt that PHP now has become the most popular WEB prescribe one of the technologies. According nexen.net survey, one-third of Internet sites selected to develop the PHP server-side program. In the U.S., Europe and Japan and other countries, PHP development market presents a scene of prosperity, such as Facebook, Yahoo!, Flickr, and such well-known PHP site Sourceforge numerous. China's major websites in recent years have gradually extensive use of PHP.

 

Rely on active, well-organized development community, PHP language itself has been steady progress - on the one hand continue to improve performance and stability, increase the range of practical tools; on the other hand actively learn the advantages of other programming languages to enrich the language features . Today's PHP, which can support a powerful object-oriented development (such as Java), it retains the easy to learn syntax (such as C), at the same time, PHP also has a very diverse range of practical functions, extensions and class libraries, very convenient for WEB Development. In addition, object-oriented development with the gradual popularization of various open source PHP class library and development framework endless.

 

By the end of June, PHP officially released PHP5.3.0. This is an unusual PHP version because it fixes a lot of Bug (over 140), but also brought a lot of PHP programmers for the long-awaited new features. Some features originally planned for PHP6 in the release, but a loud voice, in advance PHP5.3 released.

 

Let's look at what good things PHP5.3 in it.

 

1. New features in PHP 5.3

    * Support for namespaces (Namespace)

There is no doubt that the namespace is PHP5.3 brought the most important new features. With the concept of namespaces, in the development of a large site, is easier to design a flexible structure, while avoiding different package class name or variable name conflict.

In PHP5.3 before the division of practice is the way to Package the directory name to separate code file, the code in the class name is with an underscore _ to represent the directory. Such as

 

 

Code sample:

<? Php
class Zend_Db_Table_Select ()
/ / That present the class file is located in Zend / Db / Table / Select directory
?>

This naming is PEAR, Zend Framework, and various PHP projects used extensively. Although the method can be avoided in different packages or libraries in the class name conflicts, but when writing code are more cumbersome and clumsy.

In PHP5.3, the only need to specify the namespace can be different, the namespace separator for the anti-diagonals \.

 

 

 

Code sample:

<? Php
namespace Zend \ Db \ Table;
class Select ()
?>

This namespace exist even if the other class, called Select, the program in the call will not conflict. Readability is also increased.

    * Support the delay of static binding (Late Static Binding)

In PHP5, we can in the class through the self keyword or __CLASS__ to determine or call the current class. But there is a problem, if we call in the child class, get the result will be the parent class. Because the time of inheriting the parent class, static members have been bound. For example:

 

 

 

Code sample:

<? Php
class A (
    public static function who () (
        echo __CLASS__;
    )
    public static function test () (
        self:: who ();
    )
)

class B extends A (
    public static function who () (
         echo __CLASS__;
    )
)

B:: test ();
?>

Code above the output is:
A

This is different from our expectations, we want the original sub-class of the corresponding results.

PHP 5.3.0 adds a static keyword to reference the current class, which implements late static binding:

 

 

 

Code sample:

<? Php
class A (
    public static function who () (
        echo __CLASS__;
    )
    public static function test () (
        static:: who (); / / here to achieve a delay of static binding
    )
)

class B extends A (
    public static function who () (
         echo __CLASS__;
    )
)

B:: test ();
?>

Code above the output is:
B

 

    * Support for goto statement

Most computer programming languages support the unconditional shift statement goto, when the program execution to the goto statement, goto statement that is turned by the label that the procedures in place to continue. Despite the goto statement may cause the program flow is not clear, readable weakened, but in some cases with the convenience of its unique phenomena, such as interruption of the depth of nested loops and if statements.

 

 

Code sample:

<? Php
goto a;
echo 'Foo';
 
a:
echo 'Bar';

for ($ i = 0, $ j = 50; $ i <100; $ i + +) (
  while ($ j -) (
    if ($ j == 17) goto end;
  )
)
echo "i = $ i";
end:
echo 'j hit 17';
?>

    * Support for closures, Lambda / Anonymous Function

Closure (Closure) function and the concept of Lambda functions programmed in the field from the function. Such as JavaScript support closures and lambda functions in one of the most common language.

In PHP, we can also create_function () function is created when the code runs. But there is a problem: to create the function is compiled only when running, but not with the other code is compiled into executable code at the same time, we can not use this executable code like APC cache to improve performance of object code.

In PHP5.3, we can use the Lambda / anonymous function to define the temporary use (disposable type) function, as array_map () / array_walk () callback function such as function.

 

 

 

Code sample:

<? Php
echo preg_replace_callback ('~-([ az ])~', function ($ match) (
    return strtoupper ($ match [1]);
), 'Hello-world');
/ / Output helloWorld

 

$ Greet = function ($ name)
(
    printf ("Hello% s \ r \ n", $ name);
);

$ Greet ('World');
$ Greet ('PHP');

 

//... In a class

$ Callback = function ($ quantity, $ product) use ($ tax, & $ total) (
   $ PricePerItem = constant (__CLASS__. ":: PRICE_". Strtoupper ($ product));
   $ Total + = ($ pricePerItem * $ quantity) * ($ tax + 1.0);
 );
array_walk ($ products, $ callback);
?>

 

 

    * Added two magic methods __callStatic () and __invoke ()

PHP in there was a magic method __call (), when the code calls the object method does not exist a magic method that will be called automatically. New __callStatic () method only for a static class method. When there is no attempt to call a static class method, __callStatic () magic method will be called automatically.

 

 

 

Code sample:

<? Php
class MethodTest (
    public function __call ($ name, $ arguments) (
        / / $ Name parameter is case sensitive
        echo "call object method '$ name'"
             . Implode ('-', $ arguments). "\ N";
    )

    / ** PHP 5.3.0 or later in the class method is effective * /
    public static function __callStatic ($ name, $ arguments) (
        / / $ Name parameter is case sensitive
        echo "call the static method '$ name'"
             . Implode ('-', $ arguments). "\ N";
    )
)

$ Obj = new MethodTest;
$ Obj-> runTest ('by an object called');

MethodTest:: runTest ('static call'); / / As of PHP 5.3.0
?>

After the implementation of the above code the output is as follows:

  Call the object method 'runTest' - invoked by the object

Call the static method 'runTest' - static call

 

Form to call a function object, __invoke () method will be called automatically.

 

 

 

Code sample:

<? Php
class MethodTest (
    public function __call ($ name, $ arguments) (
        / / $ Name parameter is case sensitive
        echo "Calling object method '$ name'"
             . Implode (',', $ arguments). "\ N";
    )

    / ** PHP 5.3.0 or later in the class method is effective * /
    public static function __callStatic ($ name, $ arguments) (
        / / $ Name parameter is case sensitive
        echo "Calling static method '$ name'"
             . Implode (',', $ arguments). "\ N";
    )
)

$ Obj = new MethodTest;
$ Obj-> runTest ('in object context');

MethodTest:: runTest ('in static context'); / / As of PHP 5.3.0
?>

    * Added Nowdoc grammar, usage and Heredoc similar, but use single quotation marks. Heredoc you need to declare through the use of double quotation marks.

Nowdoc variable does not do any analysis, is suitable for passing a PHP code.

 

 

 

Code sample:

<? Php

 

/ / Nowdoc single quotes after PHP 5.3 support

$ Name = 'MyName';

echo <<<'EOT'
My name is "$ name".

EOT;

 

/ / The above code output My name is "$ name". ((Which variables are not parsed)

/ / Heredoc without quotation marks

echo <<<FOOBAR
Hello World!
FOOBAR;

 

/ / PHP 5.3 or later to support double quotes

echo <<<"FOOBAR"
Hello World!
FOOBAR;

 

?>

 

    * Support Heredoc to initialize static variables, class members and class constants.

Code sample:

<? Php
/ / Static variables
function foo ()
(
    static $ bar = <<<LABEL
Nothing in here ...
LABEL;
)

/ / Class member, constant
class foo
(
    const BAR = <<<FOOBAR
Constant example
FOOBAR;

    public $ baz = <<<FOOBAR
Property example
FOOBAR;
)
?>

    * Can also be used outside the class to define the constants const

PHP constants are usually defined in this way:

 

 

Code sample:

<? Php
define ("CONSTANT", "Hello world.");
?>

PHP5.3 add a constant defined by:

 

 

Code sample:

<? Php
const CONSTANT = 'Hello World';

?>

    * Ternary operator adds a quick way to write:?:

Original format is (expr1)? (Expr2): (expr3)
If expr1 result is True, the result of expr2 is returned.

PHP5.3 add a kind of writing style, you can omit the middle section, written as expr1?: Expr3
If expr1 result is True, then return the result of expr1

    * HTTP status codes in the 200-399 range were considered to be a successful visit
    * Support for dynamic invocation of static methods

Code sample:

<? Php
class Test
(
    public static function testgo ()
    (
         echo "gogo!";
    )
)

$ Class = 'Test';
$ Action = 'testgo';
$ Class:: $ action (); / / output "gogo!"
?>

    * Support nested handle the exception (Exception)
    * New garbage collector (GC), and enabled by default

2. PHP5.3 other noteworthy changes in the

 

1. A large number of bug fixes

2. PHP performance improvement

3. Php.ini variables can be used

4. Mysqlnd extend into the core theory, the expansion of access to mysql pace than in the previous MySQL and MySQLi extensions fast (see http://dev.mysql.com/downloads/connector/php-mysqlnd/)

5. Ext / phar, ext / intl, ext / fileinfo, ext/sqlite3 and ext / enchant other extensions by default with PHP bindings release. Phar which can be used to package PHP program, similar to Java in the jar mechanism.

6. Ereg regular expression functions are no longer available by default, use the faster of the PCRE regular expression functions

Conclusion:

PHP 5.3 is a great improvement in the PHP version, but it still follows the design principles of PHP - a powerful, easy to use. PHP5.3 the one hand, object-oriented development, and so be strengthened, so that PHP is more appropriate for enterprise application development on the other hand, also increased the number of useful features and a new extended syntax. We look forward to its early stabilization can, 成为 中 WEB development of another one Li Qi.

Declined comment