Where does function_exists() look to decide whether a function exists?

The documentation says:

Does the function merely scan the single php document using function_exists, looking for any functions that match?

Read More

For example if I have if ( function_exists( 'get_custom_header' ) in header.php, will the function look for get_custom_header in header.php?

Or does the function look into other directories and/or files, such as functions.php or in other files in the wordpresss core(ie. the wp-admin folder or wp-includes folder)?

Related posts

3 comments

  1. function_exists('f') will check if a function f was declared before the call to function_exists. Therefor the direct answer to your question is – in the functions section of the interpreter memory.

    The result of function_exists do not only depend on the file in which the function is declared and the file in which the call is made but on the execution path.

    function a() {}
    -- at another file executed later
    echo function_exists('a');
    
    // displays true
    

    while

    echo function_exists('a');
    // display false;
    -- at another file executed later
    function a() {}
    

    function_exists at the same file as the function definition (which is a trivial not interesting use case) will always return true because the interpreter parses all function before starting to execute the file.

    Adding to the fun is the fact that nested functions are defined only when their parent is evaluated and all php function are in the global scope which might lead to

    function a() {
      function b() {}
    }
    
    echo function_exists('b');
    
    // displays false
    

    while

    function a() {
      function b() {}
    }
    
    a();
    echo function_exists('b');
    
    // displays true
    

    In a big and complex code like wordpresss it is not trivial to predict the result of function_exists which is probably why core doesn’t use this pattern any longer. If you have the choice you should use hooks instead.

  2. When the PHP parser parses the files, from your initial index.php to the current file containing your function_exists, it reads each variable, function, class, method, property… literaly everything, and stores them in a large Hash Table. Basically, the names of those methods/variables are hashed and used as the keys.

    Now when you call function_exists it hashes the function name, and looks in that hash table for a matching key. So, it knows about everything up until the point you call the function, including what is inside every file included with include and require. But it doesn’t know anything that comes later.

  3. Yes the php function function_exists simply scan function only in the file where it is in, But not in the included files which are after the condition of function_exists. So in order you want to check the function then the function with file should be included above the function_exists condition.

    This will echo yes because the function look is included in the file before it checks the condition.

    include('ext_include.php');
    if (function_exists('look')) {
        echo "yes";
    } else {
        echo "no";
    }
    

    This will not echo yes instead it will echo no

    if (function_exists('look')) {
        echo "yes";
    } else {
        echo "no";
    }
    include('ext_include.php');
    

    NOTE: ext_include.php is the php file which has look function

    Hope this helps.

Comments are closed.