Recipe 6.10. Calling Variable Functions


6.10.1. Problem

You want to call different functions depending on a variable's value.

6.10.2. Solution

Use call_user_func( ):

function get_file($filename) { return file_get_contents($filename); } $function = 'get_file'; $filename = 'graphic.png'; // calls get_file('graphic.png') call_user_func($function, $filename);

Use call_user_func_array( ) when your functions accept differing argument counts:

function get_file($filename) { return file_get_contents($filename); } function put_file($filename, $data) { return file_put_contents($filename, $data); } if ($action == 'get') {     $function = 'get_file';     $args = array('graphic.png'); } elseif ($action == 'put') {     $function = 'put_file';     $args = array('graphic.png', $graphic); } // calls get_file('graphic.png') // calls put_file('graphic.png', $graphic) call_user_func_array($function, $args);

6.10.3. Discussion

The call_user_func( ) and call_user_func_array( ) functions are a little different from your standard PHP functions. Their first argument isn't a string to print, or a number to add, but the name of a function that's executed. The concept of passing a function name that the language invokes is known as a callback, or a callback function.

The prototype of call_user_func_array( ) comes in quite handy when you're invoking a callback inside a function that can accept a variable number of arguments. In these cases, instead of embedding the logic inside your function, you can grab all the arguments directly using func_get_args( ):

// logging function that accepts printf-style formatting // it prints a time stamp, the string, and a new line function logf() {     $date = date(DATE_RSS);     $args = func_get_args();     return print "$date: " . call_user_func_array('sprintf', $args) . "\n"; } logf('<a href="%s">%s</a>','http://developer.ebay.com','eBay Developer Program');  Sat, 23 Sep 2006 18:32:51 PDT: <a href="http://developer.ebay.com"> eBay Developer Program</a>

The logf( ) function has the same interface as the printf family: the first argument is a formatting specifier and the remaining arguments are data that's interpolated into the string based on the formatting codes. Since there could be any number of arguments following the formatting code, you cannot use call_user_func( ).

Instead, you grab all the arguments in an array using func_get_args( ) and pass that array to sprintf using call_user_func_array( ).

In this particular example, you can also use vsprintf( ), which is a version of sprintf( ) that, like call_user_func_array( ), accepts an array of arguments:

// logging function that accepts printf-style formatting // it prints a time stamp, the string, and a new line function logf() {     $date = date(DATE_RSS);     $args = func_get_args();     $format = array_shift($args);     return print "$date: " . vsprintf($format, $args) . "\n"; }

If you have more than two possibilities to call, use an associative array of function names:

$dispatch = array(     'add'      => 'do_add',     'commit'   => 'do_commit',     'checkout' => 'do_checkout',     'update'   => 'do_update' ); $cmd = (isset($_REQUEST['command']) ? $_REQUEST['command'] : ''); if (array_key_exists($cmd, $dispatch)) {     $function = $dispatch[$cmd];     call_user_func($function); // call function } else {     error_log("Unknown command $cmd"); }

This code takes the command name from a request and executes that function. Note the check to see that the command is in a list of acceptable commands. This prevents your code from calling whatever function was passed in from a request, such as phpinfo( ). This makes your code more secure and allows you to easily log errors.

Another advantage is that you can map multiple commands to the same function, so you can have a long and a short name:

$dispatch = array(     'add'      => 'do_add',     'commit'   => 'do_commit',   'ci' => 'do_commit',     'checkout' => 'do_checkout', 'co' => 'do_checkout',     'update'   => 'do_update',   'up' => 'do_update' );

6.10.4. See Also

Documentation on array_key_exists( ) at http://www.php.net/array-key-exists, call_user_func( ) at http://www.php.net/call-user-func, call_user_func_array( ) at http://www.php.net/call-user-func-array, and isset( ) at http://www.php.net/isset.




PHP Cookbook, 2nd Edition
PHP Cookbook: Solutions and Examples for PHP Programmers
ISBN: 0596101015
EAN: 2147483647
Year: 2006
Pages: 445

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net