Bug #68075 [Opn->Nab]: Functions defined in closures may not be called before definition
| From: | requinix@php.net | Date: | Mon, 22 Sep 2014 17:49:24 +0000 |
| Subject: | Bug #68075 [Opn->Nab]: Functions defined in closures may not be called before definition | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-187663@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=68075&edit=1
ID: 68075
Updated by: requinix@php.net
Reported by: fieryprophet at yahoo dot com
Summary: Functions defined in closures may not be called
before definition
-Status: Open
+Status: Not a bug
Type: Bug
Package: Scripting Engine problem
Operating System: All
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
> PHP allows calls to functions that have not yet been defined as long as they
> are defined at some point in the script.
That's only part of the rule. The rest is "...and are not defined conditionally."
http://php.net/manual/en/functions.user-defined.php
Anything conditional does not (and cannot) get that kind of special "look ahead" treatment
and will be evaluated like any other statement; same thing happens in a closure, regular function,
if, switch, etc.
Previous Comments:
------------------------------------------------------------------------
[2014-09-22 17:26:48] fieryprophet at yahoo dot com
Description:
------------
PHP allows calls to functions that have not yet been defined as long as they are defined at some
point in the script.
However, this behavior is not consistent when applied to Closures, as function calls within a
closure will fail if they are not defined before they are made even if the function definition is
contained within the same closure.
If the function in question is defined outside of a closure but after the closure is created, the
script will also proceed as expected.
Test script:
---------------
test();
function test(){
echo "Test works\r\n";
}
//output: Test works
$closure = function(){
closure_test();
function closure_test(){
echo "Closure test works\r\n";
}
};
$closure();
//expected output: Closure test works
//actual output: Fatal error: Call to undefined function closure_test()
Expected result:
----------------
Test works
Closure test works
Actual result:
--------------
Test works
Fatal error: Call to undefined function closure_test()
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=68075&edit=1