php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #61857 Extra clarification around closures required
Submitted: 2012-04-26 13:22 UTC Modified: 2014-11-18 11:03 UTC
From: jon at sprig dot gs Assigned: salathe (profile)
Status: Closed Package: Documentation problem
PHP Version: 5.3.11 OS: Irrelevant
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
49 - 22 = ?
Subscribe to this entry?

 
 [2012-04-26 13:22 UTC] 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


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-04-26 14:11 UTC] 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 14:25 UTC] 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.
 [2014-02-01 10:36 UTC] krakjoe@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: krakjoe
 [2014-02-01 10:36 UTC] krakjoe@php.net
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 :)
 [2014-02-01 13:32 UTC] salathe@php.net
-Status: Closed +Status: Re-Opened -Assigned To: krakjoe +Assigned To:
 [2014-02-01 13:32 UTC] salathe@php.net
I agree with the bug reporter, perhaps an extra intermediary example could be added showing super-simple cases of closing over variables.
 [2014-11-18 11:02 UTC] salathe@php.net
Automatic comment from SVN on behalf of salathe
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=335191
Log: Basic closure examples (doc bug #61857)
 [2014-11-18 11:02 UTC] salathe@php.net
-Status: Re-Opened +Status: Closed -Assigned To: +Assigned To: salathe
 [2014-11-18 11:03 UTC] salathe@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 30 01:01:28 2024 UTC