I never understood why in PHP each function. After all, there is foreach ...
But today I had one section of the code that had lived for six years.
There was a construction using each.
the error was fixed in 30 seconds — I had long thought to fix it all on foreach, but left it as a reminder of what kind of indocode I had at one time. But I could not just fix it and pass by.
So
each versus OOP:
1 - each ignores scopes and quietly displays private properties.
2 - each works bypassing standard interfaces like Iterator or ArrayAccess - classes that implement these interfaces are still not handled by this function.
3 - The state-class ArrayObject class is perceived by it correctly.
4 - Classes inheriting from ArrayObject also work correctly.
5 - The format of the returned data is also specific.
5.1 - property names are returned in the form of "class_name". "Property_name" (where the dot is a concatenation)
5.2 - while loop (list ($ key, $ value) = each ($ data)) returns at least some expected result
5.3 - But in reality it returns
FOUR values ​​to us.
class my { private $love = 'girl'; } $obj = new my; while ($line = each($obj)) { var_dump($line); }
Returns:
array(4) {
[1]=>
string(4) "girl"
["value"]=>
string(4) "girl"
[0]=>
string(8) "
If you, like me, have questions why, in this order of records in the LIST answer, it works “correctly”, then re-read the description to LIST carefully. As it turned out, I misunderstood the logic of LIST, and it works not in order, but by indices. This has already been written in detail, I will not stop.
')
There is a hypothesis that this behavior is due to the fact that the function receives a LINK to the array at the input.
Perhaps this is how the structure of the object is interpreted as an array ...
It is worth experimenting with inheritance and redefinition of properties, access to the parent, etc., but so far I do not see any practical application in all this. Rather, I intuitively suspect that this may lead to a more serious vulnerability than access to private properties, but I don’t know yet what to do next ...