Cool Little Features Of Php 5.4

Just got out of an energetic talk by Ilia Alshanetsky at ZendCon about the new features of the PHP5.4 release. Some of the "little ones" seemed most immediately applicable, so here's a run down of some of my personal favorites!

Accessing Values From Returned Arrays Directly

It always pissed me off when I had to do this:

$tempArray = $object->getArray();
$whatIWant = $tempArray[1];

But now, with PHP5.4, you can simply do this:

$whatIWant = $object->getArray()[1];


Printing Short Tag Availability

I'm not a huge fan of these different tags, but now you can always be certain that using the <?= ?> tags will be available for printing, and INI settings won't mess up your view scripts. You have view scripts, don't you?

Compact Array Syntax

Typing the word array can get REALLY OLD. Am I right?

$myArray = array(1,2,3);                              //Don't do this old man…
$otherArray = array('id' => 1, 'name' => 'Jason');    //...or this geezer.

$myArray = [1,2,3];                                   //This is now we do it…
$otherArray = ['id' => 1, 'name' => 'Jason'];         //...php5.4 style.

For more, go here.

JsonSerializable Interface

As Ilia explained in his talk, more and more are PHP applications being used to pass back data to a view layer for fancy processing with JavaScript. As such, it makes sense to be able to consistently pass back json data. By implementing the JsonSerializable interface, if you pass an instance of your object to json_encode, it will be transformed to json in whatever way you want:

class MessOfData implements JsonSerializable{

    //A bunch of code, including a getId and getName function

    public function jsonSerialize{
        return ['id' => $this->getId(), 'name' => $this->getName()]

See above for the minimal array syntax. Now, you can just do this for your json fun:

$myMess = new MessOfData();
echo json_encode($myMess);

For more, go here.


No need to repeat the documentation here, this is just a nice OOP way to implement your own session handler to be passed to session_set_save_handler(). Off of the top of my head, maybe you want to handle all of your user sessions with MongoDB and do it with composition. Here is an example:

class MongoUserSessionHandler implements SessionHandlerInterface{

    public function __construct(MongoUserRepository $userRepo){

    //implement the SessionHandlerInterface methods

Now, you can set your save handler in some sort of initialization event and rock and roll!

For more, go here.

Type Hinting for Callables

Let's say you have have a function that requires an anonymous function as an argument. Well, now you can type hint that with the callable hint:

$myFunction = function(){
    echo "BLAM!";

public function callFunction(callable $function){
    //call the $function as needed

Availability of $this in Anonymous Functions

Finally, PHP5.4 allows us to access the $this variable from within anonymous functions, something like this:

class myClass{
    private $name = 'Jason';

    public function sayName(){
        $name = function(){
            return $this->name;
        echo $name;

Convoluted, but you can do it!

For more, go here.

Thanks a lot for a great presentation @iliaa! I'll probably pull together a more detailed post on the "traits" feature soon enough.

If you enjoyed reading this or learned something, please consider sharing via , , or . Thanks!

If you enjoyed this article, you might like others related to the Software interest.