Doc #61857 [Com]: Extra clarification around closures required

From: Date: Thu, 26 Apr 2012 14:11:09 +0000
Subject: Doc #61857 [Com]: Extra clarification around closures required
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-8301@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=61857&edit=1 ID: 61857 Comment by: jon at sprig dot gs Reported by: jon at sprig dot gs Summary: Extra clarification around closures required Status: Open Type: Documentation Problem Package: Documentation problem Operating System: Irrelevant PHP Version: 5.3.11 Block user comment: N Private report: N New Comment: I should add, having just seen something on StackOverflow, there should be another case to list in that example: <?php $external = 'world'; $closurefunction = function ($internal) use ($external) { echo $internal . ' ' . $external . "\r\n"; } $external = 'town'; $closurefunction("hello"); // Will return "hello town" ?> I don't know if this is indeed the case, but that's what the comment here: http://stackoverflow.com/questions/1065188/in-php-5-3-0-what-is-the-function-use-identifier-should-a-sane-programmer-us#comment885456_1066219 suggests Previous Comments: ------------------------------------------------------------------------ [2012-04-26 13:22:27] jon at sprig dot gs Description: ------------ --- From manual page: http://www.php.net/functions.anonymous --- The example #3 in Anonymous functions (relating to closures and scoping) is overly complex and doesn't explain what a closure is or how it would work. Perhaps something similar to the following may be more useful? Test script: --------------- <?php $external = 'world'; $anonymousfunction = function ($internal) { echo $internal . ' ' . $external . "\r\n"; } $anonymousfunction("hello"); // Will return "hello " and probably a PHP Notice that $external hasn't been set. $closurefunction = function ($internal) use ($external) { echo $internal . ' ' . $external . "\r\n"; } $closurefunction("hello"); // Will return "hello world" $closurefunctionwithpointer = function ($internal) use (&$external) { $external = $internal; } echo $external . "\r\n"; // Will return "world" $closurefunctionwithpointer("hello"); echo $external . "\r\n"; // Will return "hello" ?> Expected result: ---------------- hello PHP Notice: Undefined variable $external hello world world hello ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=61857&edit=1

« previous php.doc.bugs (#8301) next »