Variable handling mechanism modification


The PHP7 version has made adjustments to the variable parsing mechanism. The adjustments are as follows:

1. Indirect variables, attributes and method references are interpreted from left to right:

 $$foo['bar']['baz'] // interpreted as ($$foo)['bar']['baz']
 $foo->$bar['baz']   // interpreted as ($foo->$bar)['baz']
 $foo->$bar['baz']() // interpreted as ($foo->$bar)['baz']()
 Foo::$bar['baz']()  // interpreted as (Foo::$bar)['baz']()

If you want to change the order of explanation, you can use curly brackets:

${$foo['bar']['baz']}
$foo->{$bar['baz']}
$foo->{$bar['baz']}()
Foo::{$bar['baz']}()

2. The global keyword can only refer to simple variables now

global $$foo->bar;    // 这种写法不支持。
global ${$foo->bar};  // 需用大括号来达到效果。

3. Use brackets to It is no longer useful to enclose variables or functions.

function getArray() { return [1, 2, 3]; }
$last = array_pop(getArray());
// Strict Standards: Only variables should be passed by reference
$last = array_pop((getArray()));
// Strict Standards: Only variables should be passed by reference
Note that the call in the second sentence is enclosed in parentheses, but this strict error is still reported. Previous versions of PHP would not report this error.

4. The order of array elements or object attributes automatically created during reference assignment is different from before.

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

PHP7产生的数组:["a" => 1, "b" => 1]
PHP5产生的数组:["b" => 1, "a" => 1]