PHP 5.4 Preview

The alpha release of PHP5.4 came out about a month ago. Along with the release came an announcement containing several of the more important changes, but not much in the way of detail. Here is a quick explanation of the changes highlighted in the announcement:

Added: Traits language construct

There is nothing quick about explaining traits with any level of detail. A simple explanation is that a trait is a set of reusable methods that can be referenced by a class. They are meant to resolve some of the limitations of single inheritance in PHP and prevent the need for duplicate code. I highly recommend you read the rfc for a much more thorough explanation of what traits are and how you would use them in PHP.

Added: Array dereferencing support

Again, there is an rfc detailing this change.

Probably the most common manifestation of this feature will be indexing arrays returned by functions, without actually assigning them to a variable. For example, as of PHP 5.3 you cannot do this:

function foo()
	return array('a' => 1, 'b' => 2);
echo foo()['a'];

The above code will give you the following parse error: unexpected '[', expecting ',' or ';'

If you're like me, after reading the rfc your head was spinning a bit after looking at this example:

function &foo(&$foo) {
	return $foo;
$a = array(1);
$b = foo($a)[0];
$b = 2;
var_dump($b); // array(1) {  [0]=>  int(2) }

The critical step here is that in this line:

$b = foo($a)[0];

$b is assigned a reference to the first index of $a.

Added: DTrace support

I think I'll defer to the rfc entirely for this one. I will say that this addition does not actually affect the way you write PHP. All it does is make PHP compatible with DTrace.

Improved: Improved Zend Engine memory usage and performance

You can find the details of this change in the full summary.

Moved: ext/sqlite moved to pecl

The SQLite extension is being dropped in favor of SQLite3. A similar move is being discussed regarding the MySQL extension, which will likely be dropped in favor of the PDO and MySQLi extensions, though this won't happen anytime soon.

Removed: break/continue $var syntax

I don't think I have ever seen this syntax in an actual application so chances are this change will not affect you. If you've never heard of this syntax, basically what it allows you to do is interrupt multiple levels of a control flow hierarchy. Here is a quick example:

for($i = 0; $i < 3; $i++)
	for($j = 0; $j < 3; $j++)
		if($i == 1)
			break 2;
		echo "$i:$j\n";

The above will output:

Removed: register_globals, allow_call_time_pass_reference, and register_long_arrays ini options

All of these options have been deprecated since PHP 5.3 and now they're being removed. You can find descriptions of these options here.

Removed: session_is_registered(), session_register(), and session_unregister()

Also all deprecated as of PHP 5.3. You should use $_SESSION in place of these functions.

Additional Highlights

These are something that weren't included in the announcement, but I think are worth mentioning.

<?= is now always available regardless of the short_tags setting

I have always discouraged people from using short tags because it is fairly common for them to be disabled. There is a legitimate reason for wanting to disable the <? short tag: the syntax overlaps with XML. An XML opening tag such as

<?xml version="1.0" encoding="UTF-8" ?>

would be interpreted as PHP code resulting in a parse error. The <?= tag does not share the same problem. It still won't be safe to use it in portable applications for a while, but it's something to look forward to.

Added indirect method call through array

This change allows you to call a static method using an array containing the class name and the method name. For example:

class Foo {
	static function bar()
		echo 'foobar';
$cb = array('Foo', 'bar');
$cb(); //foobar

The format of the array is the same for static method callbacks.

Again, you can find the full summary of changes here.

Leave a Reply

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


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>