note 110782 deleted from functions.anonymous by googleguy

From: Date: Fri, 07 Dec 2012 12:24:50 +0000
Subject: note 110782 deleted from functions.anonymous by googleguy
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-192383@lists.php.net to get a copy of this message
Note Submitter: l dot potherat at planetb dot fr ---- Do not try to store your anonymous function in a static variable if you use the "use" keyword. header('Content-type:text/plain'); function test($a){ static $cb=null; $stra='a'.$a; if(empty($cb)){ $cb = function ($c) use ($stra){ echo $c.$stra; }; } $cb('test '.$a.' : '); } test(1);echo "\n"; test(2);echo "\n"; This example will display test 1 : a1 test 2 : a1 If $cb is not declared as static, the example displays test 1 : a1 test 2 : a2 The "use" statement seems to copy values of the vars at the declaration time, and does not use the variable value at execution time.

« previous php.notes (#192383) next »