What Every Developer Must Know About PHP 5.4

PHP 5.4, released on March 1, 2012, is the first major update to PHP in nearly 3 years after v. 5.3 was launched in June 2009. While the much-anticipated full Unicode support has once again been kept on hold, v. 5.4 includes several changes that optimize runtime, remove deprecated features, and make code a little more graceful. As with every major new release, it’s important for every PHP developer to get up to speed on these new features.

PHP Code

What Every Developer Must Know About PHP 5.4

1. Trait Support

PHP is a single inheritance language. That is, subclasses can only inherit from a single superclass, which limits the methods that can be used across various independent classes. Hence, PHP 5.4 includes support for traits – a long overdue feature that dramatically improves the flexibility of the language and reduces repetitive, wasteful code.

Traits are essentially sets of methods that can be reused across various classes. The classic approach would entail creating two instances of the same code if a method is to be adopted across two classes. But with traits, you can create a single method and call on it in two different classes without repeating the code, which makes for leaner, more elegant code.

Examples

2. Improvements to Arrays

PHP 5.4 includes two significant improvements to arrays – support for short array syntax and dereferencing of arrays from function and method calls. Both these changes make the code easier to read and manage.

Short array syntax has been defined the PHP way in lieu of the formerly-proposed JSON syntax. Hence, an array can be defined thus:

$a = [1, 2, 3, 4];
$b = ['one' => 'two', 'three' => 'four'];

Array dereferencing, on the other hand, removes the need to define temporary variables and streamlines the code. Thus, while in earlier versions of PHP, you had to store the value from a function in a variable, and then use the variable, in PHP 5.4, you can call upon the
stored value directly. For example, earlier, you had to do this:

$food = explode(",", "pizzahut,burgerking,kfc,mcdonalds");
echo $food[3]; 	// mcdonalds

Now you can simply do this:

echo explode(",", "pizzahut,burgerking,kfc,mcdonalds")[3];	// mcdonalds 

3. $this Support in Closures

Anonymous or unnamed functions are called closures. These functions are very useful as the value of callback parameters. Prior to PHP 5.4, referring to object instances from closures required lengthy workarounds. With support for $this, you can call on any object property in any anonymous function, eliminating the need for hacks.

Example and explanation of closures on the PHP.net manual

4. Built-in Web Server

Since the focus of PHP 5.4 is to streamline the development process, it includes a built-in web server in CLI mode on port 8000 to facilitate faster development and testing, thereby eliminating the need to set up an Apache HTTPD server. This server can be called on by using a simple command:

$ cd ~/public_html
$ php –S localhost:8000

Please note that this CLI web-server is meant for testing purposes only and cannot be used for actual production.

5. < ?= Support is Always Available

The short_open_tag setting in php.ini allows for the use of < ?=. PHP 5.4 streamlines the code and includes support for < ?= by default. This essentially means that instead of writing:

<?php echo $testing ?>

You can use

<?=$testing?>

Conclusion

PHP 5.4’s changes are meant to make the code easier to read and manage, with an emphasis on removing seldom-used features (example: support for magic quotes has been dropped). The changes are evolutionary, rather than revolutionary, with the bulk of updates reserved for later iterations – most likely, the much awaited release of PHP 6.