Skip to content

Instantly share code, notes, and snippets.

@nesk
Created February 1, 2018 12:06
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 nesk/464e4477a8e205b6bc7e733eee9a1f28 to your computer and use it in GitHub Desktop.
Save nesk/464e4477a8e205b6bc7e733eee9a1f28 to your computer and use it in GitHub Desktop.
Circular references: dereferencing method
<?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