Created
May 30, 2014 20:48
-
-
Save adamloving/f0d8328d9451ccfc45ae to your computer and use it in GitHub Desktop.
Daily PHP WTF. Adding a key to an array creates a new and different array.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$a = array(); | |
$b = $a; | |
// "When using the identity operator (===), object variables are identical | |
// if and only if they refer to the same instance of the same class." | |
if ($a === $b) print "same\n"; | |
$a['test'] = 'something'; | |
if ($a === $b) | |
print "same\n"; | |
else | |
print "No longer the same object reference\n"; // Guess what, $a doesn't point to $b any more | |
// Let's try that again | |
$a = array(); | |
$b = &$a; // use ampersand to explicitly make reference a pointer | |
if ($a === $b) print "same\n"; | |
$a['test'] = 'something'; | |
if ($a === $b) | |
print "same\n"; // now they point to the same thing! | |
else | |
print "No longer the same object reference\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment