Happy Birthday PHP 7.0!

Akis Loumpourdis

PHP 7.0 was released in December 2015. Almost a year has passed since this date, which gives us a good reason to recap some of the new useful features and improvements that this release brought to the table. Some of the improvements highlighted here simply added new functionality to PHP, but they offer a nicer, more elegant way to code.

From PHP 5 to PHP 7

Yes, something is missing. What ever happened to PHP 6? Version 6 was planned to incorporate native unicode support.  For various reasons the project was abandoned around 2010. In the meantime, there was a dilemma on the next release number. A vote was held in 2014 and the decision was that the next release should be named PHP 7.The most strong case for this decision was that :

First and foremost, PHP 6 already existed and it was something completely different.  The decimal system (or more accurately the infinite supply of numbers we have) makes it easy for us to skip a version, with plenty more left for future versions to come.

(https://wiki.php.net/rfc/php6)

Features and improvements

Performance

The developers team seem to have done a great job here. The new version outperforms its predecessor, while in the same time it seems to allocate memory more effectively. Many benchmark tests have suggested a doubling of the average performance compared to PHP 5.6. This is a nice infographic highlighting comparing PHP 7 to PHP 5.6 in various popular frameworks and CMS systems (http://www.zend.com/en/resources/php7_infographic).

Error handling

Some fatal errors can now be caught and script termination can be avoided. For example, calling a method that doesn’t exist:

try {

thisIsANonExistingMethod();

} catch (Error $e) {

echo($e->getMessage());

}

This will return :

Call to undefined function thisIsANonExistingMethod()

Null coalescing operator (??)

You will definitely find this type of expressions familiar pre PHP 7:

$userName = isset($_GET[‘name’]) ? $_GET[‘name’] : ‘Anonymous’;

This can now be expressed in a more elegant way:

$userName = $_GET[‘name’] ?? ‘Anonymous’;

Spaceship operator (<=>)

This new operator  combines the “less than” ( < ), “equal” ( = ) and “greater than” operator ( > ).  It works like this:

$foo <=> $bar;

if $foo < $bar it returns “-1”; if $foo = $bar it returns “0”; and if  $foo > $bar it returns “1”.

Use define() with arrays

We can now define an array as constant.

define(

‘EMPLOYEE_NAME’, [

‘John’,

‘Michael’,

‘Helen’

]

);

Class import grouping

It is possible to group importing classes. In previous versions, we would import classes like this:

use Utilities\Validator\Url;

use Utilities\Validator\Email;

use Utilities\Validator\Date;

we can now perform the same action in a much “cleaner” way:

use Utilities\Validator\{Url, Email, Date};

Anonymous Classes

Anonymous function pre-existed in earlier versions but PHP 7 brought us anonymous classes. Let’s see a simple example:

class Checkout

{

public $order;

public function setOrder($order)

{

$this->$order = $order;

}

}

Before PHP 7

class Order

{

public function calculateTotals()

{

//Total calculation logic here

}

}

$checkout = new Checkout();

$checkout->setOrder(new Order());

In PHP 7

$checkout = new Checkout();

$checkout->setOrder(

new class

{

public function calculateTotals()

{

//Total calculation logic here

}

}

);

That was a very brief look to some of the PHP 7 improvements. There are a lot more, you can visit http://php.net/manual/en/migration70.new-features.php for more details.

Find out more about our services at Gravytrain and contact 020 3627 1970.