Features in PHP 5.4


We had to create a file upload form that allows a user to upload big files. In order to keep a good experience for the user we decided to show the progress bars.

There are several ways of doing that but it came to my mind that PHP 5.4 had an improvement on file upload, making it easier now and wondered if I had the last excuse to upgrade the servers from 5.3 to 5.4.

This is a list of some of the features PHP 5.4 comes with. There are more, but I came here with the intention of speaking about the ones that they care to me. This is not a 5.4 review, but examples.

Session upload progress

I already mentioned it, PHP 5.4 will be handy because now you can ask the server in realtime how the upload progress is going. Mixed with HTML5 you can have a kicking-ass upload form.


It seems that PHP 5.4 is faster than its ancestry, and I say that it seems because even there are plenty of sites claiming that I still have not run any benchmarks with my app.


Kids deserve  a candy sometime. We have regret many times using PHP because unlike other languages there has never been multiple inheritance. Now PHP gives us a sugar-free candy to mitigate this. Traits are not the multiple inheritane but might help you out sharing common functionallity between classes. And traits can use traits. This is an example of how a simple trait would work:

namespace Mammal\Primate;

trait Feeding
        public function getDiet( $age )
                if ( $age < 3 )
                        return [ 'breastfed' ];
                        return [ 'fruit', 'leaves', 'flowers', 'buds', 'nectar', 'seeds', 'insects', 'bird eggs' ];

Then your trait can be attached to any class like this, no matter if your class already extends something else:

class Gorilla extends VertebrateKingdom implements Animalia
        use \Mammal\Primate\Feeding;
        // ...

Now you can use the trait methods directly:

$gorilla = new Gorilla();
$gorilla->getDiet( 5 );

Python-alike stuff: lists and anonymous functions

Being a former pythonist I'm glad to have these two back in town:

Anonymous functions

Create functions on the fly: 

$dump = function( $var ) { var_dump( $var ) ; };
$dump( $gorilla->getDiet( 5 ) ); // Array containing 'fruit', 'leaves'...

Short array syntax

This is the same format than python lists, and the ability to get rid of temporary variables:

// Before: PHP < 5.3:

function getMammals()
        return array( 'monkey', 'zebra', 'dolphin', 'cat', 'John' );

$mammals = getMammals();
echo $mammals[0];

// Now: PHP 5.4+
function getMammals()
        $mammals = [ 'monkey', 'zebra', 'dolphin', 'cat', 'John' ];

echo getMammals()[0]; // Look that you don't need the temp variable anymore


Finally, now you can run a debugging server without the need apache:

 php -S localhost:8080 -t /var/www/tests

Not very useful in my case scenario where I have a full virtual environment with puppet and so on, but maybe for running temporary scripts might be useful. With earlier versions remind that you can always execute inline PHP from command line like this:

php -r "phpinfo();"