PHP 7 Migration Guide Part 1: 10 Backwards Incompatibile Changes

As we all know, PHP 7 is a new major version and stable release of PHP. It was released on 3rd December 2015. Until now 4 more enhancements have been released. Recently PHP 7.0.4 has been released, which is basically a security and bug fixes release.

PHP 7.0 is mainly all about improvement of language consistency. Before updating towards PHP 7 there are few incompatibilities and features which should be considered first before updating your web site.

This tutorial is the first part of a series about PHP 7 migration guide. In this first part we will discuss backward incompatibility issues so you can be ready to deal with these changes in the code you want to migrate to work with PHP 7.

1. Changes in Errors and Exception Handling

In PHP 7 changes have been made to default error and exception handling. Therefore, we can’ not use custom error handlers as before as now exceptions thrown for those errors.

2. Exception handlers will no longer receive just Exception objects

You can configure an exception handler to handle regular exceptions and exceptions that used to be errors. So the handler function may receive either objects of type Exception or Error.

Since both objects implement the Throwable interface, if you want to use type hinting it is better to declare your handler like this, otherwise it may cause fatal errors and not work as you intended.

void handler( Throwable $ex);

3.Variable Handling

PHP 7 utilizes an abstract syntax tree when parsing source files. It has allowed many improvements which were much needed since the previous versions.

Moreover, evaluation of indirect access now follows the left to right order. Check the following table to understand this better. Hence, the code which was written using right to left approach should be rewritten. Consider the following table from the PHP manual to better understand this concept.

Old and new evaluation of indirect expressions Expression PHP 5 interpretation PHP 7 interpretation $$foo[ ‘bar’ ][ ‘baz’ ] ${$foo[ ‘bar’ ][ ‘baz’ ]} ($$foo)[ ‘bar’ ][ ‘baz’ ] $foo->$bar[ ‘baz’ ] $foo->{$bar[ ‘baz’ ]} ($foo->$bar)[ ‘baz’ ] $foo->$bar[ ‘baz’ ]() $foo->{$bar[ ‘baz’ ]}() ($foo->$bar)[ ‘baz’ ]() Foo::$bar[ ‘baz’ ]() Foo::{$bar[ ‘baz’ ]}() (Foo::$bar)[ ‘baz’ ]()

4. list() will not follow reverse order to assign variables

From now on, variable values will be assigned using list() in the order they are defined. Practically, this change will affect the working of array [] operator.

list($a[], $a[], $a[]) = [1, 2, 3]; var_dump($a);

Output in PHP 5:

array(3) { [0]=> int(3) [1]=> int(2) [2]=> int(1) }

Output in PHP 7:

array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) }

5. Removal of Empty list() Assignments

From PHP 7 onwards, list() cannot be empty. For example these statements are invalid:

list() = $a; list(,,) = $a; list($x, list(), $y) = $a;

list() cannot unpack strings.

6. Array ordering when auto generating by referencing assignments has been changed


Originally published at www.laravelfeed.com.

Like what you read? Give LaravelFeed a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.