list()变更

1. list()不再按照相反的顺序赋值.
list($array[], $array[], $array[]) = [1, 2, 3];
----------
PHP7 $array == [1, 2, 3], 
PHP5 $array == [3, 2, 1].

注意, 只有赋值顺序变更, 而值不变. 常规用法行为不变.

list($a, $b, $c) = [1, 2, 3];
----------
$a = 1;
$b = 2;
$c = 3;
2. 不再允许空的 list() 赋值.

以下表达式无效:

list() = $a;
list(,,) = $a;
list($x, list(), $y) = $a;
3. list() 不再支持字符串解构(之前只在部分情景支持).
$string = "xy";
list($x, $y) = $string;
----------
PHP7 $x == null , $y == null
PHP5 $x == "x" , $y == "y"
4. list() 现在可以解构ArrayAccess接口对象.
list($a, $b) = (object) new ArrayObject([0, 1]);
----------
PHP7 $a == 0 , $b == 1, 
PHP5 $a == null , $b == null

Relevant RFCs:

  1. abstract_syntax_tree#changes_to_list
  2. fix_list_behavior_inconsistency

results matching ""

    No results matching ""