|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2017-09-19 14:26 UTC] requinix@php.net
-Status: Open
+Status: Not a bug
[2017-09-19 14:26 UTC] requinix@php.net
[2017-09-19 14:39 UTC] milan dot matejcek at gmail dot com
[2017-09-19 14:44 UTC] requinix@php.net
[2017-09-19 14:50 UTC] milan dot matejcek at gmail dot com
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Oct 26 22:00:01 2025 UTC |
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