Skip to content

Instantly share code, notes, and snippets.

@nesk
Last active February 1, 2018 12:02
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/d02c4e3e70d24be77b4a10585b1d306e to your computer and use it in GitHub Desktop.
Save nesk/d02c4e3e70d24be77b4a10585b1d306e to your computer and use it in GitHub Desktop.
Circular references: weak references
<?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