Tag Archives: function exists

How to Execute a Function Name String in PHP Programming 29 JUL 2013

green php elephants - elephpantsVariable function execution is a pretty handy trick, particularly if you are not assured that all objects carry the same set of shared functions.

For example you might have two similar objects but with two slightly different associated functions, meaning that when you script is presented with either of these two object, you need to execute the associated function. And while switch case statements work perfectly well for this type of example, they don’t scale up very well, meaning more often than not, it’s easier just to store the function names with the object itself.

The question now is, how do we execute a function given the function name as a string?

Well the answer lies with PHP’s built in call_user_func function that calls the provided callback (string function name), and goes even further by passing any remaining parameters into the callback call as arguments.

In practice:

function test3($message,$strings){
    echo '<p>test 3: ' . $message . '</p>';
    echo '<pre>';
    print_r($strings);
    echo '</pre>';
}

$function = 'test3';
$message = 'hello 3!';
$options = array('line 1','line 2','line 3');

if (function_exists($function)){
    call_user_func($function,$message,$options);
}

Nifty.

How to Check if Your PHP Function Exists Programming 25 JUL 2013

green php elephants - elephpantsA good way to get your PHP code to break is to reference a function that doesn’t exist (to be fair, this method of breaking code is probably valid in just about any programming language!). Luckily for us, PHP makes it fairly trivial to check that a function name does in fact exist by giving us the handy function_exists, well… function.

Basically, given a function name as a string parameter, function_exists checks the list of defined functions, both built-in (internal) and user-defined, and returns a boolean true if it finds the corresponding function.

If you then combine this function with a bit of eval magic, well then conditional function firing just got pretty easy:

function test1($message){
    echo '<p>test 1: ' . $message . '</p>';
}

function test2($message,$bold = false){
    echo '<p>test 2: ' . (($bold == false)?$message:"<strong>$message</strong>") . '</p>';
}

$function = 'test1';
$message = 'hello 1!';

if (function_exists($function)){
    eval("$function('$message');");
}

$function = 'test2';
$message = 'hello 2!';
$options = true;

if (function_exists($function)){
    eval("$function('$message',$options);");
}

Nifty.

UPDATE: Actually, instead of using the always alluring but dangerous eval to run your function, it’s probably a whole lot safer, easier, and a lot less messy, just to use PHP’s built in and very handy call_user_func function.

Meaning that now our example would look like this:

function test3($message,$strings){
    echo '<p>test 3: ' . $message . '</p>';
    echo '<pre>';
    print_r($strings);
    echo '</pre>';
}

$function = 'test3';
$message = 'hello 3!';
$options = array('line 1','line 2','line 3');

if (function_exists($function)){
    call_user_func($function,$message,$options);
}

Much better.

How to Check if Your Javascript Function Exists CodeUnit 28 MAR 2010

So I updated a panel generating script to adhere to some campaign rules, meaning that certain graphing functions were no longer being generated by the PHP script. Great, as easy as that I thought to myself as I patted myself on the back and uploaded the updated code, only to recoil in horror when the site stopped working correctly all of a sudden.

Debugging the errors, I discovered that the thing was falling over whenever a previously untouched bit of code was trying to reference my Javascript scripts – some of which were obviously no longer in existence!

So the question I was then left with is how to check for the existence of a function before actually calling that function, the solution to which turns out to be fairly simple.

All you need to do is this:

if(typeof yourFunctionName == 'function') {
yourFunctionName();
} 

where obviously yourFunctionName is the name of the function you wish to test.

Works like a charm and after implementing this on all of my function calls, no more broken site, meaning happy me and thus a well deserved pat on the back.

Nice.