note 111088 added to functions.user-defined
| From: | Anonymous | Date: | Sun, 13 Jan 2013 04:30:04 +0000 |
| Subject: | note 111088 added to functions.user-defined | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-193008@lists.php.net to get a copy of this message | ||
Note about nested functions (Example #3 Functions within functions).
You must call the parent function before the nested function is loaded and thus becomes available
for global usage. Furthermore, the nested function has no ties to its parent function or scope.
<?php
function test()
{
$a = 1;
function dosomething()
{
echo "dosomething called $a\n";
}
}
test();
dosomething(); // notice: undefined variable: a but "dosomething called\n" still
outputted
test(); // suddenly fatal error
?>
Furthermore, calling the parent function once more will result in a fatal "Cannot redeclare
funcX()" error as demonstrated by the second "test()" call above.
Consider looking into anon/closure/lambda (often functionally equivalent in phpland) instead: http://php.net/manual/en/functions.anonymous.php
----
Server IP: 69.147.83.199
Probable Submitter: 24.155.248.141
----
Manual Page -- http://php.net/manual/en/functions.user-defined.php
Edit -- https://master.php.net/note/edit/111088
Del: integrated -- https://master.php.net/note/delete/111088/integrated
Del: useless -- https://master.php.net/note/delete/111088/useless
Del: bad code -- https://master.php.net/note/delete/111088/bad+code
Del: spam -- https://master.php.net/note/delete/111088/spam
Del: non-english -- https://master.php.net/note/delete/111088/non-english
Del: in docs -- https://master.php.net/note/delete/111088/in+docs
Del: other reasons-- https://master.php.net/note/delete/111088
Reject -- https://master.php.net/note/reject/111088
Search -- https://master.php.net/manage/user-notes.php