Here at Namshi we are constanty updating our software stack to exploit the best benefits latest technology has to offer. Keeping up to date with the latest stable version of PHP, we are using PHP 5.5 for some time now. We also leverage the new features added in PHP 5.5; this post will shed light on some of the features that have come in handy during our development process.
The short array syntax was introduced in PHP 5.4, which makes creating arrays simpler than the old syntax.
For arrays like in the above example or for result sets from a database query, the array_column PHP function comes in very handy to extract out the needed data as shown below:
array_colum function is an easy yet effective way to extract out needed informaton from an array which will make working with PHP arrays pleasant than usual.
finally keyword has been added to PHP, as mentioned in the exceptions documentation on the PHP website:
Code within the finally block will always be executed after the try and catch blocks, regardless of whether an exception has been thrown, and before normal execution resumes.
This makes a very good use case when some code needs to be executed if an exception occurs and
return has to be called. A typical example is closing the database connection:
There are other use cases of finally, still beware of using it as you might find out your exception has been silenced. Use finally with care and utilize it where needed.
The DateTimeImmutable class is useful if you want the date time to stay the same for the execution context. The main instance is never modified but returns a new object instead for any operation like adding date. It works very well when you want to save the
updated_at date time columns in the database.
An example to show that even adding date to
DateTimeImmutable class does not change it:
Another example for the
updated_at use case:
There are lots of resources mentioning the new features of PHP 5.4 and PHP 5.5, I hope mentioning this useful subset helps you utilize them to solve issues.