|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2017-11-02 08:58 UTC] nobots dot nospam at example dot org
Description:
------------
Every time a closure is generated from a named function, it creates a copy of the static variables rather than modifying the original named function's ones.
function bar($k, $v){
static $foo = [];
$foo[$k] = $v;
return $foo;
}
var_dump(bar(0, 0));
var_dump(Closure::fromCallable("bar")(1, 1));
var_dump(bar(2, 2));
var_dump(Closure::fromCallable("bar")(3, 3));
$RF = new ReflectionFunction("bar");
var_dump($RF->getClosure()(4, 4));
var_dump(bar(5, 5));
the result after the last bar() call should be [0,1,2,3,4,5]
but it is [0 => 0, 2 => 2, 5 => 5]
essentially Closure::fromCallable() and $RF->getClosure() should always
return the same Closure object:
assert(Closure::fromCallable("foo") === Closure::fromCallable("foo"));
assert($RF->getClosure() === $RF->getClosure());
assert(Closure::fromCallable("foo") === $RF->getClosure());
Unless they are rebound to a different $this or cloned.
Thanks
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Oct 25 08:00:01 2025 UTC |
yes you can document the incomplete functionality if you want, but it won't make the user experience any better comparing objects seems to me a very basic feature I don't really care about statics as I rarely use them, but again I doubt the current behavior is what people expect what's the point of Closure::fromCallable("foo") then? if I did function(...$a){ return foo(...$a); } I'd at least have the static variables working