Skip to content

Instantly share code, notes, and snippets.

@clemherreman
Last active December 10, 2015 10:08
Show Gist options
  • Save clemherreman/4418974 to your computer and use it in GitHub Desktop.
Save clemherreman/4418974 to your computer and use it in GitHub Desktop.
PHP unreferencing
<?php
$a = new stdClass();
$a->prop = 'objA';
$b = new stdClass();
$b->prop = 'objB';
$c = array($a, $b);
echo 'before unsetting'.PHP_EOL;
array_map(function($obj) {
echo $obj->prop.PHP_EOL;
}, $c);
unset($b);
echo 'after unsetting'.PHP_EOL;
array_map(function($obj) {
echo $obj->prop.PHP_EOL;
}, $c);
// Output
clemherreman@macclem ~ $ php test.php
before unsetting
objA
objB
after unsetting
objA
objB // ObjB is still there because unset doesn't destroy an object, only a reference (here $b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment