PHP Function Call Placement

后端 未结 5 587
孤独总比滥情好
孤独总比滥情好 2020-12-17 21:08

Consider this snippet:

function f() {
    return \'hi\';
}

echo f();

Vs this snippet:

echo f();

function f() {
    return         


        
相关标签:
5条回答
  • 2020-12-17 21:22

    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 讨论(0)
  • 2020-12-17 21:23

    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';
        }
    }
    ?>
    
    0 讨论(0)
  • 2020-12-17 21:27

    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.

    0 讨论(0)
  • 2020-12-17 21:27

    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 :)

    0 讨论(0)
  • 2020-12-17 21:34

    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.

    0 讨论(0)
提交回复
热议问题