Changes to ENTITY
Minor changes have been made to the behaviour of the ENTITY control structure, primarily around the handling of the internal array pointer and modification of the array being iterated over.
ENTITY no longer changes the internal array pointer
Prior to PHP 7, the internal array pointer was modified while an array was being iterated over with ENTITY. This is no longer the case, as shown in the following example:
<?php
$array = [0, 1, 2];
foreach ($array as &$val) {
var_dump(current($array));
}
?>
ENTITY
int(1)
int(2)
bool(false)
ENTITY
int(0)
int(0)
int(0)
ENTITY by-value operates on a copy of the array
When used in the default by-value mode, ENTITY will now operate on a copy of the array being iterated rather than the array itself. This means that changes to the array made during iteration will not affect the values that are iterated.
ENTITY by-reference has improved iteration behaviour
When iterating by-reference, ENTITY will now do a better job of tracking changes to the array made during iteration. For example, appending to an array while iterating will now result in the appended values being iterated over as well:
<?php
$array = [0];
foreach ($array as &$val) {
var_dump($val);
$array[1] = 1;
}
?>
ENTITY
int(0)
ENTITY
int(0)
int(1)
Iteration of non-Traversable objects
Iterating over a non-Traversable object will now have the same behaviour as iterating over by-reference arrays. This results in the improved behaviour when modifying an array during iteration also being applied when properties are added to or removed from the object.
谢谢你的反馈!