This post covers some really cool PHP 5.4+ features and functions that you will most likely have missed! Some of them you may find life changing, others might just aid your development, but I can assure you that these are going to improve your PHP knowledge. All information regarding the changes in each version of PHP can be found here.

My favourite feature

I thought I’d start with my favourite new feature as I find it’s the most interesting one and can certainly speed up the development and testing of a PHP based application/website.

This feature is a command line tool which allows you to serve a site on a given host and port, this is how you would use it:

cd /path/to/project/here
php -S localhost:8000

You would then be able to access your project via http://localhost:8000 . It also shows you all the requests from within the command line; this is a must have for when you need to quickly set up a local development environment, especially if you aren’t working as part of a larger team. I’m always using it, now I don’t need to bother setting up virtual hosts or adding a new record into MAMP/XAMP/WAMP etc.

But keep in mind this is a tool which is only intended for development and testing purposes and you should not host a public website on this.

Useful PHP 5.4 features

Other than the previously mentioned feature PHP 5.4 didn’t bring much else to the table. However there are a few which I now use religiously or really find useful.

Short hand arrays

There isn’t a massive difference but when you have large expressions nested it makes it much more readable. It also follows existing syntax in other languages which makes it more recognisable.

Function array dereferencing

This is useful and can save a few lines of code, and if I’m honest I’m surprised this wasn’t already implemented.  The only thing you have to be careful of when using this is that you handle cases where an index doesn’t exist.

Useful PHP 5.5 features

Generators

We all love foreach loops but sometimes it’s easy to just pass a function which returns an array, that way we can keep our code clean and we don’t need to assign it to a variable. Well in fact that would call the function on every iteration; using up tons of unnecessary memory. Instead we want to yield the data. This means that on every iteration it will only use the memory necessary for the data that is being returned. Here is a simple example of how you might make use of this feature:

Class name resolution via ::class

Sometimes you’ll find yourself requiring a way to access the name of a class PHP now gives you an easy, intuitive way to do so.

Useful PHP 5.6 features

The introduction of another magic method: __debugInfo()

We all have the fun experience of having to debug our code and we often find ourselves calling var_dump() on objects with many properties, yet we don’t always want to see everything! This new magic method enables us to control what is outputted when we call var_dump() on our objects.

This way we can make the values of properties more readable and we can even remove ones we don’t need. Here is an example of how you might use this, I’ll give an extensive one to really show off the potential of this method!

Where next?