PHP call_user_func versus just calling function

I am sure there is a very simple explanation for this. What is the difference between the two:

function barber($type){ echo "You wanted a $type haircut, no problem\n"; } call_user_func('barber', "mushroom"); call_user_func('barber', "shave"); 

... and this (and what are the benefits?):

 function barber($type){ echo "You wanted a $type haircut, no problem\n"; } barber('mushroom'); barber('shave'); 
+66
function php
Oct 20 '09 at 17:46
source share
7 answers

Always use the name of the actual function when you know it.

call_user_func intended for calling functions whose name you do not know in advance, but it is much less efficient, since the program must look for the function at run time.

+62
Oct 20 '09 at 17:49
source share

Although you can call function variable names as follows:

 function printIt($str) { print($str); } $funcname = 'printIt'; $funcname('Hello world!'); 

There are times when you do not know how many arguments you pass. Consider the following:

 function someFunc() { $args = func_get_args(); // do something } call_user_func_array('someFunc',array('one','two','three')); 

It is also convenient for calling static and object methods, respectively:

 call_user_func(array('someClass','someFunc'),$arg); call_user_func(array($myObj,'someFunc'),$arg); 
+26
Oct. 20 '09 at 18:05
source share

there is a call_user_func option, so you can do things like:

 $dynamicFunctionName = "barber"; call_user_func($dynamicFunctionName, 'mushroom'); 

where the string dynamicFunctionName can be more exciting and generated at runtime. You should not use call_user_func if necessary because it is slower.

+12
Oct 20 '09 at 17:49
source share

I suppose this is useful for calling a function that you do not know the name in advance ... Something like:

 switch($value): { case 7: $func = 'run'; break; default: $func = 'stop'; break; } call_user_func($func, 'stuff'); 
+5
Oct 20 '09 at 17:50
source share

There are no advantages to calling such a function, because I think that it is mainly used to call the user function (for example, a plug-in), because editing the kernel file is not a good option. here is a dirty example used by wordpress

 <?php /* * my_plugin.php */ function myLocation($content){ return str_replace('@', 'world', $content); } function myName($content){ return $content."Tasikmalaya"; } add_filter('the_content', 'myLocation'); add_filter('the_content', 'myName'); ?> 

...

 <?php /* * core.php * read only */ $content = "hello @ my name is "; $listFunc = array(); // store user function to array (in my_plugin.php) function add_filter($fName, $funct) { $listFunc[$fName]= $funct; } // execute list user defined function function apply_filter($funct, $content) { global $listFunc; if(isset($listFunc)) { foreach($listFunc as $key => $value) { if($key == $funct) { $content = call_user_func($listFunc[$key], $content); } } } return $content; } function the_content() { $content = apply_filter('the_content', $content); echo $content; } ?> 

....

 <?php require_once("core.php"); require_once("my_plugin.php"); the_content(); // hello world my name is Tasikmalaya ?> 

Exit

 hello world my name is Tasikmalaya 
+1
Jan 27 '16 at 7:46
source share

in the first example, you use the function name, which is a string. it can occur from the outside or be determined "on the fly." that is, you don’t know what function you will need to run at the time the code is created.

0
Oct 20 '09 at 17:48
source share

When using namespaces, call_user_func () is the only way to run a function that you don't know beforehand, for example:

 $function = '\Utilities\SearchTools::getCurrency'; call_user_func($function,'USA'); 

If all your functions were in the same namespace, this would not be such a problem, as you could use something like this:

 $function = 'getCurrency'; $function('USA'); 

Edit: Following @Jannis, stating that I was wrong, I did a bit more tests and didn't have much luck:

 <?php namespace Foo { class Bar { public static function getBar() { return 'Bar'; } } echo "<h1>Bar: ".\Foo\Bar::getBar()."</h1>"; // outputs 'Bar: Bar' $function = '\Foo\Bar::getBar'; echo "<h1>Bar: ".$function()."</h1>"; // outputs 'Fatal error: Call to undefined function \Foo\Bar::getBar()' $function = '\Foo\Bar\getBar'; echo "<h1>Bar: ".$function()."</h1>"; // outputs 'Fatal error: Call to undefined function \foo\Bar\getBar()' } 

You can see the output results here: https://3v4l.org/iBERh it seems that the second method works for PHP 7, but not PHP 5.6.

-one
Oct 02 '13 at
source share



All Articles