Created
October 10, 2012 03:45
-
-
Save electrum/3863040 to your computer and use it in GitHub Desktop.
PHP destructors with cycles
This file contains hidden or 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 5.3.15 (cli) (Aug 24 2012 17:40:23) [Darwin] | |
>>> class Foo { public $x; function __destruct() { echo "destroy\n"; var_dump($this); } } | |
>>> $a = new Foo(); $b = new Foo(); $a->x = $b; $b->x = $a; unset($a); unset($b); | |
>>> gc_collect_cycles() | |
destroy | |
object(Foo)#2 (1) { | |
["x"]=> | |
object(Foo)#1 (1) { | |
["x"]=> | |
*RECURSION* | |
} | |
} | |
destroy | |
object(Foo)#1 (1) { | |
["x"]=> | |
object(Foo)#2 (1) { | |
["x"]=> | |
*RECURSION* | |
} | |
} |
This file contains hidden or 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 5.3.15 (cli) (Aug 24 2012 17:40:23) [Darwin] | |
>>> class Foo { public $x; function __destruct() { echo "destroy\n"; var_dump($this); } } | |
>>> $a = new Foo(); $b = new Foo(); $c = new Foo() | |
>>> $a->x = $b; $b->x = $c; $c->x = $a | |
>>> unset($a); unset($b); unset($c) | |
>>> gc_collect_cycles() | |
destroy | |
object(Foo)#3 (1) { | |
["x"]=> | |
object(Foo)#1 (1) { | |
["x"]=> | |
object(Foo)#2 (1) { | |
["x"]=> | |
*RECURSION* | |
} | |
} | |
} | |
destroy | |
object(Foo)#2 (1) { | |
["x"]=> | |
object(Foo)#3 (1) { | |
["x"]=> | |
object(Foo)#1 (1) { | |
["x"]=> | |
*RECURSION* | |
} | |
} | |
} | |
destroy | |
object(Foo)#1 (1) { | |
["x"]=> | |
object(Foo)#2 (1) { | |
["x"]=> | |
object(Foo)#3 (1) { | |
["x"]=> | |
*RECURSION* | |
} | |
} | |
} |
This file contains hidden or 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 5.3.15 (cli) (Aug 24 2012 17:40:23) [Darwin] | |
>>> class Foo { public $x; public $destroyed = false; function go() { var_dump($this->destroyed); } function __destruct() { echo "destroy\n"; var_dump($this); $this->destroyed = true; $this->x->go(); } } | |
>>> $a = new Foo(); $b = new Foo(); $a->x = $b; $b->x = $a; unset($a); unset($b); | |
>>> gc_collect_cycles() | |
destroy | |
object(Foo)#2 (2) { | |
["x"]=> | |
object(Foo)#1 (2) { | |
["x"]=> | |
*RECURSION* | |
["destroyed"]=> | |
bool(false) | |
} | |
["destroyed"]=> | |
bool(false) | |
} | |
bool(false) | |
destroy | |
object(Foo)#1 (2) { | |
["x"]=> | |
object(Foo)#2 (2) { | |
["x"]=> | |
*RECURSION* | |
["destroyed"]=> | |
bool(true) | |
} | |
["destroyed"]=> | |
bool(false) | |
} | |
bool(true) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment