The documentation says:
Does the function merely scan the single php document using function_exists, looking for any functions that match?
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)?
function_exists('f')
will check if a functionf
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.while
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
while
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.When the PHP parser parses the files, from your initial
index.php
to the current file containing yourfunction_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 withinclude
andrequire
. But it doesn’t know anything that comes later.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 thefunction_exists
condition.This will echo yes because the function look is included in the file before it checks the condition.
This will not echo yes instead it will echo no
NOTE:
ext_include.php
is the php file which haslook
functionHope this helps.