-
Notifications
You must be signed in to change notification settings - Fork 23
alternatives for "clone $GLOBALS" ? #78
Comments
Of course, can you give me input? I'm not familiar with the issue. What is the issue? How can I reproduce it? |
Hi, This code results in an fatal error: $copy = clone $GLOBALS; Output in ALL PHP versions:
To clone an array you just need to assign it to a new variable. But To create a clone of the Here are a demonstration of this issue (or run it on https://3v4l.org/VkDgZ): // Testing Globals
$GLOBALS['A'] = 'B';
$arrayCopyGlobalsVar = $GLOBALS;
$arrayCopyGlobalsVar['A'] = 'C';
var_dump([
'$GLOBALS' => $GLOBALS['A'],
'$arrayCopyGlobalsVar' => $arrayCopyGlobalsVar['A'],
]);
$GLOBALS['A'] = 'B';
$nonReferencedGlobalsVar = new ArrayObject($GLOBALS);
$nonReferencedGlobalsVar = $nonReferencedGlobalsVar->getArrayCopy();
$nonReferencedGlobalsVar['A'] = 'D';
var_dump([
'$GLOBALS' => $GLOBALS['A'],
'$nonReferencedGlobalsVar' => $nonReferencedGlobalsVar['A']
]); Output for PHP 5.4.0 - 7.4.4:
i can't understand how e00f12a could be done and why this should fix #58. I think you can reproduce this by checking an extension value like mentioned in #58. But i think you can replace this - $value = clone $GLOBALS;
+ $value = (new ArrayObject($GLOBALS))->getArrayCopy(); |
The main problem is, that a method called "getValueForKeyPath" is modifying the global state:
With the if check the structure of the return array is clear. So we don't have to overwrite the $GLOBALS here. Instead we can return an array with the serialized data like this:
In fact, this is much more readable and do what the method name pretends. |
Hey guys, thanks for your detailed feedback. Do you need a backport for 2.x? |
There are still some needs for it so it would be really nice to have a backport. |
Can we think about this again?
e00f12a#diff-85035a0cade545b687554eb75b5a191cR112
The text was updated successfully, but these errors were encountered: