Why in PHP is it possible to access the functions before the line they were declared?

Asked

Viewed 120 times

9

Why in PHP it is possible to call a function before the line in which it was declared?

echo ah(); // 'Ah!'

function ah() {
     return 'Ah!';
}

Notice that I called ah() first, then I declared. Even so, it was executed. But the function theoretically would not exist before that line? So how was it called?

How does this happen internally? I thought the script would run sequentially, but calling a function that is declared after that gives me another understanding.

This seems to work only when the statement is made in the same script.

When a script insertion occurs after the function is called, this does not happen, even if inside the include there is the function called.

index php.

echo ah();

include 'ah.php';

a. php

function ah() { return 'Ah!'; }

That would generate:

Uncaught Error: Call to Undefined Function ah() in index.php

That is: I can call a function before the declaration line, as long as it is in the same file. If it is with include, that doesn’t work.

Why?

  • What is the difference between the function declared after the call that is in the same script for the function that was called before the declaration coming from a include?

  • In the first case, PHP analyzes the code only once to know if the function was declared? How was this "magic" done?

First example in IDEONE

  • I might be wrong, but include in php is different from other languages, it’s actually like it’s a eval (in fact he is a eval) and the "scope" is often different, so much so that the include can even return values.

1 answer

11


I can’t say with authority about the exact functioning of PHP, but I know about compilers (an interpreter is still a compiler), and everyone I know who allow the function to be called before it is declared makes the process of lexing and Parsing in two steps.

First it analyzes all statements, creating a table of public symbols that can be used in script, and ignores what is in the body of functions. Then he analyzes the algorithms contained in the functions, so when the call occurs, there is already science of the statement. Simple like this.

The first step analysis does not consider the inclusion of files, so it does not resolve the symbols contained in this new file. Nor could it already that the include is dynamically resolved (at the time of execution). It can even be conditional, so only after analyzing the algorithm and the actual execution can it decide whether to include it or not, so it cannot be solved in the first step. Not even in the second, it is solved in the execution.

C and Pascal are some of the few exceptions they do not make in two steps. C++ does in several, and yet does not allow the statement out of order. I could, but maybe I could get confused because of include.

  • Ah, it makes a little more sense now. I thought he "did the job just once".

  • @Wallacemaxters I have improved.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.