Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Daily PHP WTF. Adding a key to an array creates a new and different array.
<?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