note 114066 added to class.closure
| From: | derkontrollfreak+9hy5l at gmail dot com | Date: | Wed, 08 Jan 2014 13:09:56 +0000 |
| Subject: | note 114066 added to class.closure | ||
| Groups: | php.notes php.notes | ||
| Request: | Send a blank email to php-notes+get-198154@lists.php.net to get a copy of this message | ||
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";
/*
* Result in PHP 5.3:
* ------------------
* destructed
* finished
*
* Result since PHP 5.4:
* ---------------------
* finished
* destructed
*/
?>
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 () {
};
}
?>
----
Server IP: 87.106.1.137
Probable Submitter: 85.177.248.107
----
Manual Page -- http://php.net/manual/en/class.closure.php
Edit -- https://master.php.net/note/edit/114066
Del: integrated -- https://master.php.net/note/delete/114066/integrated
Del: useless -- https://master.php.net/note/delete/114066/useless
Del: bad code -- https://master.php.net/note/delete/114066/bad+code
Del: spam -- https://master.php.net/note/delete/114066/spam
Del: non-english -- https://master.php.net/note/delete/114066/non-english
Del: in docs -- https://master.php.net/note/delete/114066/in+docs
Del: other reasons-- https://master.php.net/note/delete/114066
Reject -- https://master.php.net/note/reject/114066
Search -- https://master.php.net/manage/user-notes.php