PHP 7
June 19, 2020

PHP 7: Features and Performance Comparison

PHP Development

PHP 7 was a massively redesigned PHP release, offering new language constructs, significant performance improvements, and lower resource utilization. This continues to make it a natural choice for fast-paced business-critical applications.

In this article, we look at PHP 7 features, performance comparisons, end of life, major releases, and why the PHP 7 release marked such a big change for PHP.

Back to top

What Is PHP 7?

PHP 7, released to the public in 2015, followed upon the success of PHP 5.6, and delivered new features and significant performance and resource utilization improvements.

Release Date

PHP 7.0 was released on December 3, 2015. The most current version of PHP available, PHP 7.4, was released on November 28, 2019. PHP 7.4 is expected to be the last release of PHP in the 7 series, with PHP 8 expected later in 2020.

End of Life

PHP 7.0 and 7.1 have both reached end of life. PHP 7.2 reaches end of life on November 30, 2020. Assuming PHP 7.4 is the last version of PHP in the 7 series, PHP 7 will reach total end of life on November 28, 2022.

Major Releases

PHP 7 has had four major releases since the release of PHP 7.0 in 2015:

Back to top

PHP 7 Features

PHP 7 introduced dozens of new features, and huge performance boosts for the community. PHP 7 provided the community with a true high-powered language that can compete in the modern space with any other programming language while introducing some features that many considered to be "missing". 

The most exciting new features of PHP 7 it is now 2x faster than PHP 5. PHP 7 adds static type hinting, which makes possible many of the tools now available for static analysis and static error detection.  

Anonymous Classes

Anonymous classes are a huge step forward for testability in PHP. For example, have you ever wanted to test an abstract class, but didn't want to extend it? Or needed to change the visibility of a property or method to make it testable? Wanted to test a trait? Anonymous classes are what you need for that.

<?php

trait SomeTrait {
    public function addNumbers($a, $b)
    {
        return $a + $b;
    }
}

$anonymous = new class {
    
    use SomeTrait;
    
}

In this example, we've defined a trait called SomeTrait, and a public method called addNumbers(). In order to test this, we need to actually put the trait inside of a class. But rather than defining a class, we can simply use the trait inside an anonymous class! Then we can call the method we want to test and verify that it works.

Anonymous classes can receive constructor arguments, meaning they can receive other objects or dependencies that they require to do their work. Anonymous classes can be nested if necessary.

Scalar Type Hints

Most developers are familiar with the concept of type hints when it comes to type hinting on objects and arrays. Type hinting is useful for indicating the object, interface, or presence of an array that we desire. PHP 7 took this to a next level and included the ability to include scalar type hints for strings, integers, Booleans, and floats. 

The scope of the changes is vast. and you'll want to consult the manual to ensure you fully understand the changes. In general, PHP will attempt to coerce the type you selected, unless strict typing is turned on. The available type hints (in addition to object class names) are self, array, callable, bool, float, int, and string. 

Static type hinting is incredibly useful for enforcing the type of argument you want passed to a particular function, and for validating your intent when working with code. For example, if you permit strings and integers, but do arithmetic on the arguments, that could generate a bug. Enforcing an integer only helps ensure that you're receiving the type that will work with your intended design.

Return Type Hints

Along with scalar type hints, we also got return type hints. Return type hints allow you to specify the return type that you expect for a given function or method of an object.

<?php

class MyClass
{
    public function myMethod() : string
    {
        return "hello world!";
    }
}

Here we've defined a class that contains a method that returns a string. We know it returns a string because we have defined the return type. If we inadvertently return anything else, say an integer or a boolean, PHP will complain, and an error will be generated.

In general, it's a good idea to return type hint all of your methods as much as you can, and stick with a single return type declaration (using exceptions for events that are out of scope for your method to handle). 

Engine Exceptions

In PHP 7, the engine moved to using exceptions under the hood for raising fatal errors. A Throwable class was introduced for exceptions, and all exceptions use the Throwable class to raise their exception. From there, PHP branches exceptions into two categories: errors and the traditional Exception class we've all come to know.

Error is further extended to various types of error like ParseError and TypeError. You can type hint for, and catch, these exceptions as appropriate. Just bear in mind you should generally not catch all exceptions, including parse errors. However, you can catch all errors by catching the Throwable exception interface.

Throwable cannot be extended by userland developers. 

Back to top

New Syntax Features in PHP 7

PHP 7 introduced a large number of new syntax features that are incredibly helpful for developers to know and use on a daily basis. This "syntactical sugar" will make development in PHP 7 considerably easier.

Group Import Declarations

Namespaces were introduced in PHP 5.3, and have become popular for the development of PHP applications. There are times when you want to introduce multiple classes from the same namespace. Prior to PHP 7 this was a cumbersome process involving tens or dozens of lines of boilerplate at the beginning of a file. In PHP 7, we received group import statements.

A group import statement is just what it sounds like: we group what we're importing, so the base namespace is the same, and we list out what we're importing.

<?php

// THIS

use My\Great\Namespace\ClassA;
use My\Great\Namespace\ClassB;
use My\Great\Namespace\ClassC;
use My\Great\Namespace\ClassD;

// BECOMES THIS

use My\Great\Namespace\{ClassA, ClassB, ClassC, ClassD};

Most of the modern PHP IDE's support the use of group import.

Null Coalescing Operator

Another bit of sugar added to PHP 7 was the ability to use a null coalescing operator. This operator assigns a variable based on whether or not the first value is null. For example:

<?php

$username = $_POST['user'] ?? 'no name specified';

The benefit of this is that it shortens the syntax required to check if a value is null and assign something else. 

Spaceship Operator

The spaceship operator is a fairly simple tool for comparing two expressions. The spaceship operator compares expressions by determining whether the left-most parameter ($a) is less than, equal to, or greater than the right-most parameter ($b), and returns -1 (less than), 0 (equal to), or 1 (greater than). It can be used to compare integers, floats, and even strings. 

Back to top

PHP 7 Performance

PHP 7 dramatically improves the performance of PHP, in many cases coming close to or even doubling the performance of an application over PHP 5.x. This performance enhancement is possible due to the tremendous work of the PHP core team, specifically in their rewrite of the underlying components of PHP to make them faster. Each incremental release of PHP 7 provides further boosts in performance over PHP 7.0.

  • Drupal - Compared to previous versions, Drupal 8 runs 72 percent faster on PHP 7 when compared to previous versions. 
  • Wordpress - For WordPress, a PHP 7 runtime only executes 25M CPU instructions compared to just under 100M doing the same job on older PHP versions.
  • Magento - For Magento, servers running PHP 7 are able to serve up to three times more requests as those running PHP 5.6.

The benchmarks were also confirmed with larger enterprise applications. When the Tumblr team rolled out PHP 7, they experienced a latency drop of 50 percent, and their CPU load decreased at least 50 percent, often more.

Back to top

Additional Resources

Want to learn more about the latest  major PHP 7 release? The webinar below does a quick dive on the new features and performance improvements in PHP 7.4.

Looking for further reading on other PHP versions? These articles are a great place to start:

Get Long Term Support for PHP 7

Zend by Perforce helps businesses with planning, managing, and maintaining their PHP applications on end-of-life PHP versions. Take advantage of frequent bug fixes and security updates with ZendPHP Enterprise. ZendPHP Enterprise is a certified PHP runtime, with improved security and mission-critical, SLA-backed support for better PHP websites.

GET STARTED 

Back to top