PHP 7.2

It is that time of year again. A new version of PHP has been unleashed into the world.

I think this one is a relatively low-key release in terms of language changing features. A few small things here and there but nothing too big. But here is a selection of changes.

A full list with further details can be found here.

Overview of Versions

So now is a good time to take stock of what versions of PHP are currently supported.

If you are on anything less than PHP 5.6, you are in trouble and you have been for a while. Security support for 5.5 ended some time in the middle of 2016.

PHP 5.6 is no longer getting active support, but is going to to continue to get security support until 31st December 2018. This means that bugs are only going to be fixed if they pose a security threat. It also means that you have also got just over a year to make the leap to PHP 7.

If you are on 5.6, it really isn't that much of a big jump to get to PHP 7 in terms of backwards incompatible changes.

PHP 7.0 ends its active support in 2 days on the 3rd of December 2017 and will continue to get security fixes until the same time next year. So if you are still on this version, I would suggest skipping 7.1 and upgrading straight to 7.2 and now is the time to start thinking about doing this.

PHP 7.1 has active support for another year and security support until today in 2019.

PHP 7.2 obviously has 2 years of active support ahead of it now and a year of security support following that.


Override Argument Type Annotations

Let's say that I have the following class:

class MyClass
    public function myFunction(int $var)
        return $var;

It has a single function called myFunction that takes a single argument that has been specified as being an integer. All it does is return the argument as it is.

Now if I make another class that extends the one above:

class MyChildClass extends MyClass
    public function myFunction($var)
        return $var;

I have overridden the function, myFunction and although it does the same thing, it no longer has the type hint.

If I now use this class:

$obj = new MyChildClass;

This would give me the expected result but it will come with a warning:

PHP Warning:  Declaration of MyChildClass::myFunction($var) should be compatible with MyClass::myFunction(int $var) 

This warning will no longer occur from PHP 7.2 onwards. I am not sure how useful this feature will be. In my mind, it could lead to laziness when inheriting from libraries as it is no longer forcing you to use a type.

But apparently it "complies with contravariance of method argument types under the Liskov Substitution Principle". Who am I to argue with Liskov?

Override Abstract Function With Another Abstract Function

Let's say I have an abstract class:

abstract class MyClass
    abstract function myFunc(int $param);

And another that inherits it:

abstract class MyOtherClass extends MyClass
    abstract function myFunc(int $param):int;

In my inherited class, I override myFunc with another abstract function and even give it a return type.

Now if I implement this abstract class and use it:

class FinalClass extends MyOtherClass
    public function myFunc(int $param):int
        return ($param * $param);
$obj = new FinalClass;

I would get an error:

PHP Fatal error:  Can't inherit abstract function MyClass::myFunc() (previously declared abstract in MyOtherClass)

You could not inherit an abstract class in this way. From PHP 7.2, you can. So the above code would run successfully and give me the result:

Trailing Comma in Group Use Statements

From PHP 7.0, you have had the ability to group use statements like so:

use SomePackage\{

But if you had a trailing comma like this:

use SomePackage\{

You would get a parse error:

Parse error: syntax error, unexpected '}', expecting identifier (T_STRING) or function (T_FUNCTION) or const (T_CONST)

From PHP 7.2, you are now allowed to have the trailing comma. This makes it possible to do cleaner diffs when doing source management. It is why it is often down in long arrays (I personally hate it though).

The original RFC had a few different places to have the trailing comma allowed but this was the only one that was accepted.

Object Type Annotation

PHP 7.2 brings another type that you can use to type hint with called 'object'.

Take the following:

class MyClass
     public static function myFunc(object $param)
         return $param;

If I try to use the function and pass it a generic object:


I get a fatal error:

Fatal error: Uncaught TypeError: Argument 1 passed to MyClass::myFunc() must be an instance of object, instance of stdClass given

Because it is expecting some object that is an instance of a class called "object". But as of 7.2, "object" is a reserved word, and the code runs correctly.

This is a very useful addition as it gets rid of the need for the is_object check that one would normally have to do in this case.

MCrypt Now In The Crypts

This is actually something removed rather than added and I have included it as it is the biggest change that effects me personally.

In PHP 7.1, the Mcrypt extension was deprecated. Now it is completely removed. This is because libmycrypt has not been maintained for 10 years! So it is now considered insecure and even if you are not planning upgrade, you should stop using it.

Alternatives that you can use are OpenSSL or, new to 7.2, libsodium. The functions are very similar so porting across should be straightforward.

If you absolutely can not migrate away from it but really want to upgrade to 7.2, it is still available as a PECL extension so could be installed separately (but you should seriously just scrap it completely).


So there are just a few of the features added (and removed) in this latest iteration of PHP. You can read about these and the rest of the release notes here on the PHP website.

© 2012-2022