Last active
February 1, 2018 12:02
-
-
Save nesk/d02c4e3e70d24be77b4a10585b1d306e to your computer and use it in GitHub Desktop.
Circular references: weak references
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 | |
class ParentClass | |
{ | |
public function __construct() | |
{ | |
$this->child = new ChildClass($this); | |
} | |
} | |
class ChildClass | |
{ | |
public function __construct(ParentClass $parent) | |
{ | |
$this->parent = new WeakRef($parent); | |
} | |
} | |
$instance = new ParentClass; | |
// Do your work... | |
// Once the parent instance is unreferenced, the parent and child instances will be destroyed. | |
unset($instance); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment