Hudzilla.org - the homepage of Paul Hudson
Contents > Functions Wish List | Report Bug | About Me ]

4.19     Variable functions: is_callable(), call_user_func() and call_user_func_array()

This is NOT the latest copy of this book; click here for the latest version.

bool is_callable ( mixed function_name [, bool syntax_only [, string callable_name]])

mixed call_user_func ( callback function [, mixed parameter [, mixed ...]])

mixed call_user_func_array ( callback function [, array parameters])

As you have seen already, PHP has variable variables so it is not surprising we have variable functions. This particular piece of clever functionality allows you to write code like this:

<?php
    $func
= "sqrt";
    print
$func(49);
?>

PHP sees that you are calling a function using a variable, looks up the value of the variable, then calls the matching function. The code above will therefore return 7 - the square root of 49.

As variable functions are quite unusual and also easy to get wrong, there is a special PHP function, is_callable(), that takes a string as its only parameter and returns true if that string contains a function name that can be called using a variable function. Thus, our script becomes this:

<?php
    $func
= "sqrt";
    if (
is_callable($func)) {
        print
$func(49);
    }
?>

As an alternative to variable functions, you can use call_user_func() and call_user_func_array(), which take the function to call as their first parameter. The difference between the two is that call_user_func() takes the parameters to pass into the variable function as multiple parameters to itself, whereas call_user_func_array() takes an array of parameters as its second parameter.

This next script demonstrates both of these two performing a functionally similar operation, replacing "monkeys" with "giraffes" in a sentence using str_replace():

<?php
    $func
= "str_replace";
    
$output_single = call_user_func($func, "monkeys", "giraffes", "Hundreds and thousands of monkeys\n");
    
$params = array("monkeys", "giraffes", "Hundreds and thousands of monkeys\n");
    
$output_array = call_user_func_array($func, $params);
    echo
$output_single;
    echo
$output_array;
?>

Although call_user_func() is essentially the same as using a variable function, call_user_func_array() is very helpful for functions that have complex and variable parameter requirements. One popular application for variable functions is to allow other developers using your code to register callbacks - they pass in the name of the function they want your code to call, then you can use call_user_func() to execute that.





<< 4.18 Recursive functions   4.20 Callback functions >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Be the first to add a comment to this chapter!



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is nine plus eight?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow