Beware that since PHP 5.4 registering a Closure as an object property that has been instantiated in the same object scope will create a circular reference which prevents immediate object destruction:
<?php
class Test
{
private $closure;
public function __construct()
{
$this->closure = function () {
};
}
public function __destruct()
{
echo "destructed\n";
}
}
new Test;
echo "finished\n";
?>
To circumvent this, you can instantiate the Closure in a static method:
<?php
public function __construct()
{
$this->closure = self::createClosure();
}
public static function createClosure()
{
return function () {
};
}
?>