1

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.

asked Mar 18, 2017 at 15:31
1
  • are you running nested2.php separately from nested1.php? Commented Mar 18, 2017 at 15:35

3 Answers 3

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

Codemole
3,1995 gold badges28 silver badges42 bronze badges
answered Mar 18, 2017 at 16:50
1
  • Thanks for the info, but this does not answer the question I asked. Commented Mar 20, 2017 at 13:39
2

Example 1: PHP does not support nested functions. It only supports inner functions. Nested function reference

Inner 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";
 }
?>
answered Mar 18, 2017 at 15:38
3
  • I disagree; PHP does support nested functions. Edited my question to clarify. Commented 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 what inner function states. I am updating my answer with the reference of nested functions and inner functions Commented 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...) Commented Mar 18, 2017 at 18:31
0

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.

answered Mar 18, 2017 at 15:44
1
  • PHP does support nested functions. See edited question. Commented Mar 18, 2017 at 16:04

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.