note 128951 added to functions.anonymous

From: Date: Fri, 13 Oct 2023 11:46:50 +0000
Subject: note 128951 added to functions.anonymous
Groups: php.notes 
Request: Send a blank email to php-notes+get-220910@lists.php.net to get a copy of this message
"If this automatic binding of the current class is not wanted, then static anonymous functions may be used instead. " The main reason why you would not want automatic binding is that as long as the Closure object created for the anonymous function exists, it retains a reference to the object that spawned it, preventing the object from being destroyed, even if the object is no longer alive anywhere else in the program, and even if the function itself doesn't use $this. <?php class Foo { public function __construct(private string $id) { echo "Creating Foo " . $this->id, "\n"; } public function gimme_function() { return function(){}; } public function gimme_static_function() { return static function(){}; } public function __destruct() { echo "Destroying Foo " . $this->id, "\n"; } } echo "An object is destroyed as soon as its last reference is removed.\n"; $t = new Foo('Alice'); $t = new Foo('Bob'); // Causes Alice to be destroyed. // Now destroy Bob. unset($t); echo "---\n"; echo "A non-static anonymous function retains a reference to the object which created it.\n"; $u = new Foo('Carol'); $ufn = $u->gimme_function(); $u = new Foo('Daisy'); // Does not cause Carol to be destroyed, // because there is still a reference to // it in the function held by $ufn. unset($u); // Causes Daisy to be destroyed. echo "---\n"; // Note that Carol hasn't been destroyed yet. echo "A static anonymous function does not retain a reference to the object which created it.\n"; $v = new Foo('Eve'); $vfn = $v->gimme_static_function(); $v = new Foo('Farid'); // The function held by $vfn does not // hold a reference to Eve, so Eve does get destroyed here. unset($v); // Destroy Farid echo "---\n"; // And then the program finishes, discarding any references to any objects still alive // (specifically, Carol). ?> Because $ufn survived to the end of the end of the program, Carol survived as well. $vfn also survived to the end of the program, but the function it contained was declared static, so didn't retain a reference to Eve. Anonymous functions that retain references to otherwise-dead objects are therefore a potential source of memory leaks. If the function has no use for the object that spawned it, declaring it static prevents it from causing the object to outlive its usefulness. ---- Server IP: 45.112.84.4 Probable Submitter: 45.91.157.134 (proxied: 125.239.197.216) ---- Manual Page -- https://php.net/manual/en/functions.anonymous.php Edit -- https://main.php.net/note/edit/128951 Del: integrated -- https://main.php.net/note/delete/128951/integrated Del: useless -- https://main.php.net/note/delete/128951/useless Del: bad code -- https://main.php.net/note/delete/128951/bad+code Del: spam -- https://main.php.net/note/delete/128951/spam Del: non-english -- https://main.php.net/note/delete/128951/non-english Del: in docs -- https://main.php.net/note/delete/128951/in+docs Del: other reasons-- https://main.php.net/note/delete/128951 Reject -- https://main.php.net/note/reject/128951 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#220910) next »