PHP Functions Calling Variable Functions - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP Functions Calling Variable Functions - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Friday, May 24, 2019

PHP Functions Calling Variable Functions

PHP Functions




Calling Variable Functions

Problem

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

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, $d) {
                   return file_put_contents($filename, $d); }

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

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 call_user_func_array() function 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');


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. Because 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 phpin fo(). 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'
            );



No comments:

Post a Comment

Post Top Ad