PHP优先级函数声明

I have a question on the way that functions are declared in PHP.

First test :

File "functions.php" => functions toto(){ return "1"; }

Main File

    include("functions.php")
    functions toto(){ return "main"; }
    echo toto();

Second test

File "functions.php" => functions toto(){ return "1"; }

File "functions2.php" => functions toto(){ return "2"; }

Main File

    include("functions.php")
    include("functions2.php")
    echo toto();

Results

The first test work and echo "main"
The second test doesn't work => fatal error "function toto already define"
I make complementary tests :

  • in first test : put the functions toto() before include doesn't change the result.
  • Create twice functions toto() in the main file create Fatal Error


Someone can explain me how exactly this work ?
Thanks for reading

The PHP statements from the include family do not copy-paste the content of the included file in the context of the includer. The inclusion happens at runtime.

In your first example, the function toto() defined in the main file is created during the compilation. Then, on the execution, the functions.php file is read and parsed. It generates an error because it attempts to define the function toto() that is already defined.

The same happens in the second example during the inclusion of functions.php. Also, you get the same error if you declare the function toto() two times in the main script.

Either way, the PHP functions and constants cannot be re-declared. A quick quote from the documentation:

PHP does not support function overloading, nor is it possible to undefine or redefine previously-declared functions.

You can check if a function is already defined (to avoid defining it again) by using the function_exists() PHP function:

function toto() { return 1; }

if (! function_exists('toto')) {
    function toto() { return 2; }
}

Points to the topic

  • You can only name one function toto() else you get Fatal error: Cannot redeclare You can use if(!function_exists('toto')){ /*declaration*/ } to prevent that.
  • php complies a file complete before including next files, means all functions in a php will be declared, then includes are made. So if the first line includes a file that declares toto(), but the next line declares also toto(). The declaration in the include throw the Fatal error.
  • The only way to prevent this is for example wrap in the first file if(1){ } around the declaration, so know the Fatal Error comes not from the included file.

Test Case:

//file1.php
<?php
function toto(){ echo __FILE__; }
//file2.php
<?php
include 'file1.php';
function toto(){ echo __FILE__; }
toto();

Call: php file2.php

Result: Fatal error: Cannot redeclare toto() (previously declared in file2.php)

//file1.php
<?php
function toto(){ echo __FILE__; }
//file2.php
<?php
include 'file1.php';
if(1){
    function toto(){ echo __FILE__; }
}
toto();

Call: php file2.php

Result: Fatal error: Cannot redeclare toto() (previously declared in file1.php)