Created
February 1, 2018 12:06
-
-
Save nesk/464e4477a8e205b6bc7e733eee9a1f28 to your computer and use it in GitHub Desktop.
Circular references: dereferencing method
This file contains 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); | |
} | |
public function destroy() | |
{ | |
$this->child->destroy(); | |
} | |
} | |
class ChildClass | |
{ | |
public function __construct(ParentClass $parent) | |
{ | |
$this->parent = $parent; | |
} | |
public function destroy() | |
{ | |
// By setting the parent property to null, you remove the circular reference. | |
$this->parent = null; | |
} | |
} | |
$instance = new ParentClass; | |
// Do your work... | |
// Before unreferencing the parent, call the destroy method. | |
$instance->destroy(); | |
// Unreference the parent | |
unset($instance); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment