New features of PHP 5.4

PHP 5.4 is out for many months now and companies have slowly started adopting as well.

Language evolution is a constant phenomenon and it will keep happening for years to come ! Some version have minor impact and few may have dramatic impact on  coding, design & impementation.

PHP 5.4 offers mix of both. After introduction of  Namespaces in 5.3, few important changes are made in PHP 5.4

Here is summary of list of new features in PHP 5.4. Even a small syntax cange or addition may go long way. :)

  • Memory and Performance Improvements
    • Let’s say – Same code can work now about 7 to 10% faster ! Oneone will complain about that, I guess :)
  • Traits
    • I have written little bit about Traits on same blog.Traits is probably the most talked about new feature in PHP 5.4. Other languages may refer to them as “mixins” – or they may not name them at all but have an extended interface mechanism that allows the interface to contain an actual implementation of its methods.
  • Short Array Syntax

A simple, but very popular syntax addition:

$a = [1, 2, 3];
$b = ['foo' => 'orange', 'bar' => 'apple'];

That is, you now no longer need to use the ‘array’ keyword to define an array.

  • Function Array De-referencing
    • Another oft-requested syntax addition. Function calls that return an array can now be de-referenced directly:
  • Instance Method Call
    • Related to function array dereferencing, you can now call a method on object instantiation. And as in previous versions, you can of course still chain method calls, so you can now write code like this:
  • Closure Binding
    • Closures were introduced in PHP 5.3, but in 5.4 we have refined how they interact with objects.
  • Objects as Functions
    • There is a new magic method called “__invoke” which can be used in very effective way to have  ”interceptor” and “LifeCycle Managment” type of  functionality.
  • Native Session Handler Interface -
    • As a small convenience feature, there is now a session handler interface you can implement. Now you can just pass an instance of your session handling object to session_set_save_handler() instead of having to pass it six ugly function names:

SessionHandler implements SessionHandlerInterface {
public int close ( void )
public int destroy ( string $sessionid )
public int gc ( int $maxlifetime )
public int open ( string $save_path , string $sessionid )
public string read ( string $sessionid )
public int write ( string $sessionid , string $sessiondata )
}
session_set_save_handler(new MySessionHandler);

  • JsonSerializable Interface
  • Binary Notation -
    • To go along with PHP’s native support for hexadecimal and octal there is now also binary notation.
  • Improved Error Messages – Error messages are slightly improved.

While all features are great improvement and  ”step-foward”, I personally consider  Introduciton of Traits and addition of  ”__invoke” to magic methods more significant.

You can find more details and examples  here, which is article by Rasmus Lerdorf himself – http://www.oracle.com/technetwork/articles/dsl/lerdorf-php54-1564639.html

Feel free to comment on few features of PHP 5.4.  Which is your favourite one ?


Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*

Find us on Facebook

?