2

The documentation says:

Checks the list of defined functions, both built-in (internal) and user-defined, for function_name. ( SOURCE: http://php.net/manual/en/function.function-exists.php)

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

2

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.

| improve this answer | |
  • if(function_exists('do_the_thing')) { die('you are wrong'); } function do_the_thing() { echo 'thing is done'; } do_the_thing(); - Guess what's actually displayed? – Dissident Rage Jun 24 '15 at 14:31
0

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.

| improve this answer | |
  • This is helpful information. Yet it is not clear to me how one could trace exactly what should be in the hash table at any given point(in the context of using function_exists in wordpress).For example, in my example code, I know that if(function_exists('get_custom_header')); will somehow look into my theme's functions.php file. Being rather new at this, I don't see any Include or require that specifies functions.php – Daniel Dropik Dec 6 '13 at 13:56
0

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.

| improve this answer | |
  • 3
    This answer is wrong. It is not only in the file where it is run. It is in every file scanned before the function is called. – Twifty Dec 5 '13 at 9:20
  • The function from other file become exist once that is included in the file(contains function_exists()). In this file it is checking for the functions and says yes. Because here it is included in the file so it is exists.Correct me if am wrong @Waldermort – sun Dec 5 '13 at 11:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.