PHP Classes

PHP 7 Migration Guide Part 1: 10 Backwards Incompatibile Changes

Recommend this page to a friend!
  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog PHP 7 Migration Guide...   Post a comment Post a comment   See comments See comments (7)   Trackbacks (0)  

Author:

Viewers: 4,967

Last month viewers: 6

Categories: PHP Tutorials

PHP 7 is out since December 2015 and several updates were released. Its advantages compare to PHP 5.x are huge but first you need to consider what are the backwards incompatible changes so you can evaluate the effort that it may take you, so it does not break your existing applications.

Read this article to understand 10 of the most relevant backwards incompatible changes that you need to pay attention before you migrate to PHP 7.




Loaded Article
Picture of Atif Shahab Qureshi
By Atif Shahab Qureshi Pakistan

cloudways.com/en/

<email contact>

Contents

Introduction

10 Backwards Incompatibilities

Conclusion

Introduction

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.

PHP 7 logo

10 Backwards Incompatibilities

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
ExpressionPHP 5 interpretationPHP 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.

<?php

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:
<?php

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

The order of the array elements has been changed when generated by reference. Consider the following example:
<?php

$array = [];
$array["a"] =& $array["b"];
$array["b"] = 1;
var_dump($array);

?>
Output in PHP 5:
array(2) {
["b"]=>
&int(1)
["a"]=>
&int(1)
}
Output in PHP 7:
array(2) {
["a"]=>
&int(1)
["b"]=>
&int(1)
}

7. The global Keyword will only be used by simple variables:

Global keyword cannot be used anymore with variable variables. In fact curly braces should be used from now on. In short, it is discouraged to use something other than bare variable with the global keyword. Consider the following example:
<?php

function f() {
// Valid in PHP 5 only.
global $$foo->bar;
// Valid in PHP 5 and 7.
global ${$foo->bar};
}

?>

8.Changes in foreach

foreach does not support the internal array pointer any more. Before PHP 7, whenever an array was traversed using foreach, the internal array pointer used to be modified. In PHP 7 that does not happen anymore. Consider the following example:
<?php

$array = [0, 1, 2];
foreach ($array as &$val) {
var_dump( current( $array ));
}

?>
Output in PHP 5:
int(1)
int(2)
bool(false)
Output in PHP 7:
int(0)
int(0)
int(0)

9. foreach By Reference is Now Improved

Foreach performance was improved as it will track changes made during iteration. To better understand this, consider the following example:

<?php

$array = [0];
foreach ($array as &$val) {
var_dump($val);
$array[1] = 1;
}

?>
Output in PHP 5:
int(0)
Output in PHP7
int(0)
int(1)

10. Changes to Division by 0

In previous versions of PHP, when something is divided or modules by 0, false was returned. In PHP 7, such operation will return NAN, -INF or +INF. Modulus operator % will no longer issues a E_WARNING, as it will throw a DivisionByZeroError. Consider the following example:
<?php

var_dump(3/0);
var_dump(0/0);
var_dump(0%0);

?>
Output in PHP 5:
Warning: Division by zero in %s on line %d
bool(false)
Warning: Division by zero in %s on line %d
bool(false)
Warning: Division by zero in %s on line %d
bool(false)
Output in PHP 7:
Warning: Division by zero in %s on line %d
float(INF)
Warning: Division by zero in %s on line %d
float(NAN)
PHP Fatal error: Uncaught DivisionByZeroError: Modulo by zero in %s line %d

Conclusion

In this post, I have presented 10 backward incompatible changes in PHP 7 so you can have an idea of how much work you will have when migrating your code to PHP 7. In the next part of this tutorial, I will describe new features of PHP 7.

If you liked this article or have any questions about the PHP 7 backwards incompatible changes, post a comment here.




You need to be a registered user or login to post a comment

1,611,040 PHP developers registered to the PHP Classes site.
Be One of Us!

Login Immediately with your account on:



Comments:

2. mysql extension removed from php 7 - Nadir Latif (2016-04-28 17:53)
the deprecated mysql extension has been removed from php 7... - 4 replies
Read the whole comment and replies

1. Seemless - Makhtar Diouf (2016-03-22 07:27)
Pretty safe incompatibilities for me... - 1 reply
Read the whole comment and replies



  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog PHP 7 Migration Guide...   Post a comment Post a comment   See comments See comments (7)   Trackbacks (0)