Calling a function before it's defined | PHP

后端 未结 3 1121
鱼传尺愫
鱼传尺愫 2020-12-10 02:41

Is there any possible way when in one file - please note, just one file. To call a function when it isn\'t defined yet, e.g.



        
相关标签:
3条回答
  • 2020-12-10 03:23

    You cannot call undefined function, it will raise a fatal error. although in procedural code it can be called and afterwards defined. As the script is first parsed then executed. includes don't matter, they behave as if they were written in the exact file.

    there's no such thing as a variable "from a file". if the code defines the variable is not run, it can't be there.

    0 讨论(0)
  • 2020-12-10 03:32

    You can call a function which is defined after calling it. That's because PHP first parses the file and then executes it.

    As for the variable - this is not possible, you have to include the file.

    0 讨论(0)
  • 2020-12-10 03:37

    I just discovered that you can call a function if it's defined later in the same file.
    But if it's defined in an other file, you must include the file before calling the function.

    my_func();
    function my_func() {...}
    --->   No problem
    

    but

    my_func();
    include_once 'define_my_func.php';
    --->   PHP Fatal error
    

    It's like a conditional function as in the example 2 on the doc on user-defined functions

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