note 114066 deleted from class.closure by tularis

From: Date: Wed, 08 Jan 2014 13:29:00 +0000
Subject: note 114066 deleted from class.closure by tularis
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-198156@lists.php.net to get a copy of this message
Note Submitter: derkontrollfreak+9hy5l at gmail dot com ---- 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 () { }; } ?>

« previous php.notes (#198156) next »