note 110782 added to functions.anonymous

From: Date: Fri, 07 Dec 2012 10:24:26 +0000
Subject: note 110782 added to functions.anonymous
Groups: php.notes 
Request: Send a blank email to php-notes+get-192382@lists.php.net to get a copy of this message
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. ---- Server IP: 69.147.83.199 Probable Submitter: 72.52.91.2 (proxied: 81.56.254.193) ---- Manual Page -- http://php.net/manual/en/functions.anonymous.php Edit -- https://master.php.net/note/edit/110782 Del: integrated -- https://master.php.net/note/delete/110782/integrated Del: useless -- https://master.php.net/note/delete/110782/useless Del: bad code -- https://master.php.net/note/delete/110782/bad+code Del: spam -- https://master.php.net/note/delete/110782/spam Del: non-english -- https://master.php.net/note/delete/110782/non-english Del: in docs -- https://master.php.net/note/delete/110782/in+docs Del: other reasons-- https://master.php.net/note/delete/110782 Reject -- https://master.php.net/note/reject/110782 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#192382) next »