Press "Enter" to skip to content

New Features in PHP 5.4

PHP 5.3 had only just come out, and while PHP 6 was in gestation, PHP 5.4 released its first alpha version.
5.4 mainly includes the following features:

Added: Traits language construct
Added: Array dereferencing support
Added: DTrace support
Improved: Improved Zend Engine memory usage and performance
Moved: ext/sqlite moved to pecl (sqlite3 support is still built-in)
Removed: break/continue $var syntax
Removed: register_globals, allow_call_time_pass_reference, and register_long_arrays ini options
Removed: session_is_regisitered(), session_registered(), and session_unregister()

Let's focus on the three newly added points:

Traits language construct

Traits are not a new concept; c++ and java have similar things, it's just that this time PHP has brought it in too.
Traits are a lightweight form of method reuse (relative to inheritance). Why do I say that? Think of it this way: if you provide a base class for users to inherit, then unavoidably you have to consider how to make it usable after inheritance, how to avoid users overriding some key property or method and causing an error. At this point you have to consider whether to use public or private, or protected. You also have to consider that a method should be declared FINAL..
But if you use Traits, you don't need to worry about these problems. It's a form of composition — the material you provide is, in any place, a self-contained whole.

 <?php
 trait SayWorld {
   public function sayHello() {
     echo 'Hello World!';
   }
 }
 class MyHelloWorld extends Base {
   use SayWorld;
 }
 $o = new MyHelloWorld();
 $o->sayHello(); // Hello World

Conflicts between a Trait, inheritance, and a same-named function in the current class have a fixed resolution: the current class's method overrides the Trait's same-named method, and a method in the Trait overrides the base class's same-named method. For example:

 <?php
 class Base {
   public function sayHello() {
     echo 'Hello ';
   }
 }
 trait SayWorld {
   public function sayHello() {
     parent::sayHello();
     echo 'World!';
   }
 }
 class MyHelloWorld extends Base {
   use SayWorld;
 }
 $o = new MyHelloWorld();
 $o->sayHello(); // echos Hello World

For more information about Traits, see: Traits for PHP RFC

Array dereferencing support

This is really a good thing. With Array dereferencing, the old way of writing it is no longer necessary:

<?php
    list($name,) = explode(",", "Laruence, male");
?>

Instead, it's replaced by:

  $name = explode(",", "Laruence,male")[0];

Also, according to Cataphrac, Array dereferencing can also appear on the left-hand side of an assignment. That is, in theory you can write:

explode(",", "Laruence,male")[3] = "phper";

But maybe a friend would ask: what's the point of that? Hehe, at the time I raised this same question to Cataphrac, but I later learned that there are some scenarios where it can be used. For example, you first need to call a function to do some preliminary processing, then adjust the result:

<?php
function &ref(&$arr) {
    return $arr;
}
$arr = array(1,2,3);
ref($arr)[4] = 4;
var_dump($arr);
?>

DTrace support

DTrace is a performance analysis tool that can trace out data such as function call points and return points. I'm not very familiar with this either; those interested can refer to PHP 5.3.99-DEV AND DTRACE PART I

Webserver SAPI

Finally, PHP 5.4 also added a new SAPI. This SAPI supports using PHP directly as a Webserver:

$ php -S localhost:8000 test.php
//output:
PHP Development Server is listening on localhost:8000 in foo/ ... Press Ctrl-C to quit.

This feature really excites me, because about half a month earlier, I had been discussing with a few of my colleagues whether we could make something similar to reduce the Webserver layer.
However, talking with philip, I learned that this new SAPI missed the alpha1 version. It should be released in the next test version and the final version. It also doesn't support patching directly into versions before php 5.4 (this is a bit of a tragedy — if you want to use it, it seems you can only tinker with it yourself). For more information about the PHP Webserver SAPI, see: webserver sapi

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.