It appears that PHP normally requires nested functions to be defined before usage. But code built dynamically with require
doesn't have the same restriction. Can anyone explain why the inconsistency?
EDIT: Just to clarify, what I want to understand is: why does example 2 work instead of failing like example 1?
Example 1
If this is the contents of file nested1.php
:
<?php
function outer() {
inner();
function inner() {
print "Hello world.\n";
}
}
outer();
?>
Running this with php nested1.php
returns:
PHP Fatal error: Call to undefined function inner() in nested1.php on line 3
However, if you move the inner()
function call below the function definition like this:
<?php
function outer() {
function inner() {
print "Hello world.\n";
}
inner();
}
outer();
?>
and run again you get:
Hello world.
Example 2
If this is the contents of nested2.php
:
<?php
function outer() {
require "inner.php";
}
outer();
?>
And this is the contents of inner.php
:
<?php
inner();
function inner() {
print "Hello world.\n";
}
?>
Running this with php nested2.php
returns:
Hello world.
3 Answers 3
When outer()
function is called first time, the inner()
function inside of it will get declared in global scope.
function outer() {
function inner() {
print "Hello world.\n";
}
inner();
}
outer();
outer();//Second call
Thus you'll get the following error:
Fatal error: Cannot redeclare inner()
because the second call to outer()
tried to re declare inner()
function.
In order to avoid this issue, you need to use anonymous function declaration like following:
function outer() {
$inner = function () {
print "Hello world.\n";
};
$inner();
}
outer();
outer();
In this case $inner
available only in local function "outer" scope
-
Thanks for the info, but this does not answer the question I asked.Kruthers– Kruthers2017年03月20日 13:39:13 +00:00Commented Mar 20, 2017 at 13:39
Example 1:
PHP does not support nested functions. It only supports inner functions.
Nested function reference
<?php
function outer() {
inner();
function inner() {
print "Hello world.\n";
}
}
outer();
Example 2: In this function you are just requiring file which includes and evaluates the scripts.
Reference
<?php
function outer() {
require "inner.php";
}
outer();
?>
<?php
inner();
function inner() {
print "Hello world.\n";
}
?>
-
I disagree; PHP does support nested functions. Edited my question to clarify.Kruthers– Kruthers2017年03月18日 16:03:38 +00:00Commented Mar 18, 2017 at 16:03
-
@Kruthers In your
Example 1
According to first part of your PHP code you are calling function before its definition. It will be accessible if PHP has support for nested function. But in your second part you are able to call function after its definition. This is whatinner function
states. I am updating my answer with the reference of nested functions and inner functionsSahil Gulati– Sahil Gulati2017年03月18日 17:46:18 +00:00Commented Mar 18, 2017 at 17:46 -
@SahilGulati Ok thanks for the clarification. So the term "nested" means something specific that PHP is not actually doing in this case. But the link you provided doesn't help me understand why example 2 works differently. For example, inner.php would have access to variables defined in outer() before the require statement. So PHP can't evaluate inner.php until after it's imported. It seem like it should act like example 1 (in my meager understanding at least...)Kruthers– Kruthers2017年03月18日 18:31:23 +00:00Commented Mar 18, 2017 at 18:31
In your first example, you are trying to define a new function inside another function, but nested functions are not allowed by php. Doing something like this would be fine:
<?php
function outer() {
inner();
}
outer();
function inner() {
print "Hello world.\n";
}
?>
In your 2nd example, you are including a file. As you are not using nested functions in inner.php, the code works as expected.
-
PHP does support nested functions. See edited question.Kruthers– Kruthers2017年03月18日 16:04:27 +00:00Commented Mar 18, 2017 at 16:04
nested2.php
separately fromnested1.php
?