PHP 8.0: New Features and Incompatibilities

PHP 8.0.0 was released at the end of November 2020, and it brings some exciting changes and a few major backwards compatibility breaks.

Arguments

The first big change is named arguments; this is something you frequently see with Python, Ruby, Kotlin, VB, etc. Some functions take a lot of arguments, but many of them are optional. For example, if you want to use htmlspecialchars and set double_encode you would need to pass four arguments. However, with named arguments, you can now pass just two.

// 8.0
htmlspecialchars($string, double_encode: false);
// 7.x htmlspecialchars($string, ENT_COMPAT | ENT_HTML401, 'UTF-8', false);

Union Types

Php 7.0 brought strict typing for parameters and return types. Php 7.1 added on that allowing nullable and void return type, and 7.2 added a generic object. 8.0 has added union types. If your function expects or returns more than one type you can define it rather than allowing anything. The new syntax uses a pipe, so you can write something like the line below to accept an int or float and return an int or float.

function foo(int | float $bar) : int | float

Nullsafe Operator

The feature I am most excited about is the nullsafe operator ?->.  It allows you to access a member variable or call a function, and if the object you are calling is null, it will return null.  If you have been working with objects (and you should), then there has likely been a time where you wrote some code like this:

$firstName = null; 
 if (!is_null($object) 
     && !is_null($object->getOtherObject()) 
     && !is_null($object->getOtherObject()->getName())
 ) { 
     $firstName = $object->getOtherObject()->getName()->getFirstName();
 }

With the nullsafe operator, that code can be written as:

$firstName = $object?->getOtherObject()?->getName()?->getFirstName();

JIT Compiler

Finally, PHP has added – just in time – a compiler to the OPcache extension. While preloading a feature  released with 7.4 tends to have a bigger impact on applications written using large frameworks loading a lot of files, the JIT compiler adds a little extra boost based on some benchmarks.

Backward Incompatible Changes

With Every PHP release there tends to be some backwards incompatible changes, and 8.0.0 being a major release is no different. PHP 5 added __construct as a new magic function for constructors before that the constructor was a function with the same name as the class. PHP 7 deprecated the old class name constructor, but if you have any old classes or libraries that have not been updated to the magic function, the class will break with PHP 8.

Calling nonstatic methods statically has been removed as well. The last change that I could see break code in a fun way is a change to == with non-numbers. You should always use triple equals. However, if you use double with mixed types, PHP 8 may cause unexpected issues. As always for a full list of changes read the migration documentation on php.net.

Support for PHP 7

Currently php 7.4 is scheduled to stop receiving security updates on November 28, 2022.  They could extend support much like what was done for php 5.6. However, that is currently not the plan, so you have two years to update your code to work with PHP 8.0! If you need assistance, our team can help. Contact us to get started.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top