Skip to content

Instantly share code, notes, and snippets.

@Strate
Created August 14, 2014 08:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Strate/b037e529ee752489a45f to your computer and use it in GitHub Desktop.
Save Strate/b037e529ee752489a45f to your computer and use it in GitHub Desktop.
PHP refcount bug
<?php
class B {
static public $instances = [];
public function __destruct()
{
echo "Object B destructed!";
}
}
$b = new B; // Here positive testing
B::$instances[spl_object_hash($b)] = &$b;
xdebug_debug_zval('b');
echo "Expecting object destruction ... ";
$b = false; // Object destructed here as expected;
echo " Expectation finished\n";
class A {
static public $instances = [];
public function __construct()
{
self::$instances[spl_object_hash($this)] = &$this;
}
public function __destruct()
{
echo "Object A destructed!";
}
}
$a = new A; // Here negative testing
xdebug_debug_zval('a');
echo " Expecting object destruction ... ";
gc_collect_cycles();
$a = false; // Object is not destructed here.
echo " Expectation finished\n";
echo "End of script\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment