InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Calling Variable Functions in PHP
PHP

Calling Variable Functions in PHP

InfinityCoder November 23, 2016

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

Use call_user_func():

1
2
3
4
5
6
7
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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);

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():

1
2
3
4
5
6
7
8
9
10
// 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:

1
2
3
4
5
6
7
8
9
// 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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$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:

1
2
3
4
5
6
$dispatch = array(
   'add'       => 'do_add',
   'commit'    => 'do_commit',    'ci' => 'do_commit',
   'checkout'  => 'do_checkout',  'co' => 'do_checkout',
   'update'    => 'do_update',    'up' => 'do_update'
);

 

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Deleting Elements from an Array in PHP
You want to remove one or more elements from an …

Deleting Elements from an Array in PHP

Program: HTTP Range in PHP
The program in Example 8-22 implements the HTTP Range feature, …

Program: HTTP Range in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap