PHP 5.3: Overview

    Days go by, the weather improves, but meanwhile PHP 5.3 is getting closer and closer - you have to be fully armed.
    RC2 came out recently, then there will be RC3, and then Stable (about a month later).

    The first thing I want to say: 5.2 is slower than 5.1, but 5.3 is faster than 5.1. In 5.3, many optimizations (including link tables) were introduced, and this is good news.

    There are no special problems of backward compatibility, with the exception of the introduction of new reserved words and other minor points.
    They are described in detail here - wiki.php.net/doc/scratchpad/upgrade/53

    In addition, there are many innovations:

    1. mysqlnd.
    As you know, PHP communicates with the MySQL server via C-wrapper over libmysql, however the implementation has a huge overhead (for example, when you do mysql_fetch_assoc, the result has as many as three copies in memory).
    MySQL Native Driver is an efficient C-level replacement for libmysql. However, interfaces (mysql, mysqli, PDO) should not suffer from this in any way.

    2. Closures.
    Are you tired of seeing create_function () under the rim of your toilet?
    $lambda = function() {echo 'Hello World!';};
    $lambda();

    * This source code was highlighted with Source Code Highlighter.

    You can also do:
    class myLambda
    {
      public function __invoke() {echo 'Hello World!';}
    }
    $lambda = new myLambda;
    $lambda();

    * This source code was highlighted with Source Code Highlighter.
    Moreover, you can use variables from the scope of the context inside the function:
    $var = 'Hello World!';
    $func = function() use ($var) {echo $var;};
    $func();

    * This source code was highlighted with Source Code Highlighter.
    This is very convenient to do when using preg_replace_callback, and when setting events.

    Using $ this in closure code is not allowed. This was originally planned, but the developers had technical problems.
    However, you can pass a reference to the object using use, this will allow you to access public interfaces.

    Manual at wiki.php.net/rfc/closures

    3. namespaces. Namespaces
    namespace hello;
    class msg
    {
     public static function write() {echo 'Hello';}
    }
    msg::write();
    namespace World;
    class msg
    {
     public static function write() {echo ' World!';}
    }
    msg::write();

    * This source code was highlighted with Source Code Highlighter.
    An example is specially cumbersome. namespace'y convenient to use for the logical union of classes, so that the constants do not intersect, and in many other cases. I recommend to use.
    The namespace separator in the path is the backslash (\).

    Manual at php.net/namespaces

    4. Ternary operator.
    It can be used without specifying a true alternative, then the value itself becomes it.
    $var = 'Hello World!';
    echo $var?:'false';
    // Hello World!

    * This source code was highlighted with Source Code Highlighter.
    However, in this I see very little practical application.

    5. Labels. GOTO analogue.
    Labels are named positions in the code to which you can make a transition, of course you cannot switch between levels, and each has its own scope of Labels.
    $i = 1;
    start:
    echo ($i > 1?'-':'').$i;
    if ($i++ < 5) {goto start;}
    echo ' вышел зайчик погулять';
    // 1-2-3-4-5 вышел зайчик погулять

    * This source code was highlighted with Source Code Highlighter.
    Useful thing when creating non-trivial algorithms.
    Oh, itching fingers to write a code obfuscator :)

    6. Garbage Collector.
    Finally, a normal garbage collector appeared in PHP, that is, it will be possible to write long-running scripts without memory leaks.

    7. SPL.

    Firstly: http://www.php.net/~helly/php/ext/spl/

    And besides, about significant innovations: SplFixedArray, SplStack, SplDoublyLinkedList, SplQueue, SplPriorityQueue. Some new data structures under certain conditions give a performance boost.

    You can view information about Spl classes, interfaces, and functions in phpinfo () or from the command line:
    $ php -i | less
    $ php -r 'var_export (spl_classes ());'
    $ php --re spl | less

    Thanks crocodile2u.
    Thanks to Sherman81 for the tip.

    8. Late Static Binding The
    __callStatic magic method and the get_called_class () function have appeared.
    This is a very useful innovation.

    In addition, they updated PCRE to 7.9 (a bug fix was announced), sqlite improved, and much more.

    Surely I did not take into account something important, please politely poke my nose into it, I’ll add it.
    I invite everyone to take part in testing!

    Also popular now: