PHP 5.4 Features

PHP 5.4 Features

PHP 5.4

PHP 5.4.0 is planned to be released on February, 2 2012. By the time you are reading this, it may already been out. It is a result of many months of development.


Many features were proposed for this release. Some made into this version, others did not make it at least for now. So, now you may be wondering which interesting features really made it. Let me tell you more about some of the more interesting features present in this release.


PHP does not support multiple inheritance. This means that unlike languages such as C++, it is not possible to create one class by inheriting the behavior of multiple other classes.

However, PHP implements the support to have classes with multiple interfaces since version 5. This is a simple approach inspired in Java that avoids the ambiguity problems of C++ multiple inheritance implementation.

The problem of using either multiple inheritance or multiple interfaces is that it becomes easy for creating bloated classes that have much more inherited functions than they may really need in practice.

Traits provide a simpler alternative. You can define a trait practically the same way you define a class or an interface with whatever functions you need. Then you use them in a new class you want to have those functions.

Here is an example taken from the original proposal RFC document:

<?php trait ezcReflectionReturnInfo { function getReturnType() { /*1*/ } function getReturnDescription() { /*2*/ } } class ezcReflectionMethod extends ReflectionMethod { use ezcReflectionReturnInfo; /* ... */ } class ezcReflectionFunction extends ReflectionFunction { use ezcReflectionReturnInfo; /* ... */ } ?>

Discontinued Features

Some PHP releases discontinue past features. That happens especially in more important releases like this. Usually features are discontinued after have been made deprecated in past versions.

In PHP 5.4 magic quotes is a feature that was discontinued. It used to be enabled if you had certain options set in php.ini file.

It was meant to automatically escape values for use in SQL queries. The problem is that the use of that feature made third party PHP components unreliable, as they would need to check if the option was set to determine if they need to escape the values to use SQL queries.

Furthermore, correct literal value escaping depends on the database you are connecting to. So it is more reliable to have the magic quotes options disabled and escape the values explicitly, eventually using database specific functions for that purpose, like for instance mysql_real_escape_string().


APC caching extension

PHP compiles the code into Zend opcodes before executing. This makes PHP run much faster than when it was an interpreted language before the PHP 4 days.

However, the opcode compilation process still consumes a significant amount of time, specially if you code uses a lot of external component scripts. This overhead can be avoided if you use a caching extension. If you are using PHP in a busy Web server, using an opcode caching extension is mandatory.

There are many caching extensions. Some are Open Source, others are commercial. APC is an Open Source caching extension developed by several core PHP developers.

Despite the importance of using an opcode caching extension, in PHP 5.4.0 APC is still not shipping in the main PHP distribution.

It was planned initially to make it in PHP 6, but the original plans for that major release were canceled. Then it was anticipated for PHP 5.4, but at least for PHP 5.4.0, APC is claimed to not be ready for this version.

This is not good news for PHP. Frequently we see language benchmark comparing PHP with other languages used on the Web. Very often the PHP installation used in the benchmarks is running without an opcode cache, while the other languages use their own caching mechanisms.

It is an unfair comparison but many people are not aware about these details so they believe PHP is really slower. PHP often appears to be losing because the benchmarks are not done with PHP running with a caching extension.


Annotations are a form of adding metadata to your code. The metadata information can be used by those tools for instance to produce additional support code, so you do not have to write such code manually. One common use is to generate PHP code or SQL statements to map data between class objects and database tables.

There were several proposals to implement annotations in PHP, very similar to the way they work in other languages like Java and C#. Those proposals ended up not being accepted or fully implemented to make into PHP 5.4.

Still, there are alternative approaches that consist in separate annotation parser tools that extract annotation metadata from PHP comments.

Array short syntax

The popularity of JSON made it a common format for exchanging serialized data between code eventually written on different languages.

One basic difference between JSON and PHP literal value format is that arrays are represented by brackets instead the usual array() construct. Some people proposed to add support for a shorter syntax similar to JSON, but the proposal was not approved by PHP core developers.

Leave a comment