Bug #75228 [NEW]: anonymous function keep context if does not need

From: Date: Tue, 19 Sep 2017 14:16:03 +0000
Subject: Bug #75228 [NEW]: anonymous function keep context if does not need
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-211243@lists.php.net to get a copy of this message
From: milan dot matejcek at gmail dot com Operating system: linux PHP version: 5.6.31 Package: Class/Object related Bug Type: Bug Bug description:anonymous function keep context if does not need Description: ------------ I tried on version php5.6-7.2 here is same behavior like in test script. I create anonymous function in class body, in function i don't need context $this but anonymous function keep the reference to Foo object in memory until end of run time. Foo class has not defined variable by myself. In test script is hack like class bar. Test script: --------------- class MyClass { public $event; } class Foo { public function __construct(MyClass $bar) { $bar->event = function () { // this keep reference on $this, when i don't use $this }; } public function __destruct() { echo __CLASS__ . ' '; } } // hack how remove reference for context class Bar { public function __construct(MyClass $bar) { $bar->event = self::createAnonymousFunction(); } private static function createAnonymousFunction() { return function () { // this does not keep reference }; } public function __destruct() { echo __CLASS__ . ' '; } } $myClass1 = new MyClass(); $myClass2 = new MyClass(); new Foo($myClass1); new Bar($myClass2); echo "Hello "; Expected result: ---------------- Foo Bar Hello Actual result: -------------- Bar Hello Foo -- Edit bug report at https://bugs.php.net/bug.php?id=75228&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=75228&r=trysnapshot54 Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=75228&r=trysnapshot55 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=75228&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=75228&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=75228&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=75228&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=75228&r=needscript Try newer version: https://bugs.php.net/fix.php?id=75228&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=75228&r=support Expected behavior: https://bugs.php.net/fix.php?id=75228&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=75228&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=75228&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=75228&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=75228&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=75228&r=dst IIS Stability: https://bugs.php.net/fix.php?id=75228&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=75228&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=75228&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=75228&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=75228&r=mysqlcfg

« previous php.bugs (#211243) next »