Doc #61857 [Opn->Csd]: Extra clarification around closures required
| From: | krakjoe@php.net | Date: | Sat, 01 Feb 2014 10:36:19 +0000 |
| Subject: | Doc #61857 [Opn->Csd]: Extra clarification around closures required | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-10924@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
Updated by: krakjoe@php.net
Reported by: jon at sprig dot gs
Summary: Extra clarification around closures required
-Status: Open
+Status: Closed
Type: Documentation Problem
Package: Documentation problem
Operating System: Irrelevant
PHP Version: 5.3.11
-Assigned To:
+Assigned To: krakjoe
Block user comment: N
Private report: N
New Comment:
I'm not able to see what the documentation looked like for 5.3, having reviewed the
documentation of today, I do not see a problem in the description or any of the examples provided.
So, I'm going to close the bug.
Feel free to report a new bug, for a supported, stable version of PHP, if the information is still
not satisfactory in some way.
Thanks for taking the time to make PHP better :)
Previous Comments:
------------------------------------------------------------------------
[2012-04-26 14:25:18] jon at sprig dot gs
Errr, oops, that was supposed to show
$closurefunction("hello"); // You may expect "hello town", but it will return
"hello world" still because the use ($vars) is as per when it is defined, not when it is
called.
------------------------------------------------------------------------
[2012-04-26 14:11:09] jon at sprig dot gs
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
------------------------------------------------------------------------
[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