3 PHP 5.5 features we are using at Namshi
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.
PHP 5.4 and PHP 5.5 both were feature packed PHP releases, we have found 3 of these features useful in our routine PHP development which are discussed below:
array_column function
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:
The PHP 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
Finally, the 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.
DateTimeImmutable
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 created_at
and 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 created_at
and 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.