note 114079 added to functions.anonymous

From: Date: Thu, 09 Jan 2014 16:41:03 +0000
Subject: note 114079 added to functions.anonymous
Groups: php.notes 
Request: Send a blank email to php-notes+get-198172@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.212 ---- Manual Page -- http://php.net/manual/en/functions.anonymous.php Edit -- https://master.php.net/note/edit/114079 Del: integrated -- https://master.php.net/note/delete/114079/integrated Del: useless -- https://master.php.net/note/delete/114079/useless Del: bad code -- https://master.php.net/note/delete/114079/bad+code Del: spam -- https://master.php.net/note/delete/114079/spam Del: non-english -- https://master.php.net/note/delete/114079/non-english Del: in docs -- https://master.php.net/note/delete/114079/in+docs Del: other reasons-- https://master.php.net/note/delete/114079 Reject -- https://master.php.net/note/reject/114079 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#198172) next »