开发者

PHP Function Call Placement

Consider this snippet:

function f() {
    return 'hi';
}

echo f();

Vs this snippet:

echo f();

function f() {
    return 'hi';
}

When I run the scripts, they both produce the same results. That's great.

But my question is (and I could not find a good answer or reference in any o开发者_运维百科f my searches), is it okay to call the function before it is defined (ie, from a sequential parsing of the script file perspective)?

I don't want to run into any issues or deprecation down the road if I leave the function call ahead of the function definition block in my script file.


From the Manual:

Functions need not be defined before they are referenced, except when a function is conditionally defined as shown in the two examples below.

The possibility to call (reference) a function before it is defined is a PHP intentional feature and I don't think you need to worry about it becoming deprecated.

As an observation, if you can choose from declaring the function before or after, it would be common sense to declare it before it's used.

Note: The following code will give a fatal error because the function will only be defined at run rime.

<?php
echo helloWorld();
if(1){
    function helloWorld() {
        return 'hello world';
    }
}
?>


compiler steps are like so:

  • Converts a sequence of characters into tokens
  • Analyses the tokens to determine there Grammatical structure.
  • Generates byte code depending on the outcome of the analyses

So the easiest way to understand this is just because the script is not multi threaded does not mean its processed in one in line execution.

PHP Reads your entire source code into tokens before its executed, there for it has control over the order of tokens should be executed first.

Take this example

while(true)
{
    print '*';
}

Each line is a sequence of characters, so PHP Would interpret this as

if          #T_IF
            #T_WHITESPACE
(
            #T_WHITESPACE
true        #T_STRING
            #T_WHITESPACE
)
            #T_WHITESPACE
{
            #T_WHITESPACE
print       #T_PRINT
            #T_WHITESPACE
'*';        #T_CONSTANT_ESCAPED_STRING
            #T_WHITESPACE
}

but just because its been read does not mean its been executed.

So that functions are at the top of the list, this way you can execute them because there already within the systems memory.

I believe that the reason for this is that PHP's native library such as PFO,mysql_connect functions and classes are loaded first, and they move all user defined scopes to be loaded after there native implementations.

there loaded at the beginning of execution.


This is such a great question. Because it doesn't have a really good answer. PHP will, if given the chance, work just fine doing it backwards. Until it doesn't. And it won't if, for example, the function is defined in a yet-to-be loaded included file later on. PHP will include those files as they happen in code, so you will get a function not defined error in that case.

This is one SERIOUS gotcha in PHP.

It helps to imagine that includes are like copy/pasting whatever was in the other file into the code. But that it only happens when they get run in code. Which means they can be dynamic and based on the running code. But it also means they can't be pre-processed and linked up before-hand.


I find it a good practice to first define my functions and later call them, but it doesn't matters where do you put them as long they're there ;)

Also, i like to have my functions separated in different php files, depending on the use, just to be organized :)


No matter where you define your function and where you call. Because as far as I know, PHP server application first reads the whole page then executes it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜