PHP call_user_func 与只调用 function

我相信有一个很简单的解释,它们之间的区别是什么:

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

... 还有这个(有什么好处?) :

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

in your first example you're using function name which is a string. it might come from outside or be determined on the fly. that is, you don't know what function will need to be run at the moment of the code creation.

the call_user_func option is there so you can do things like:

$dynamicFunctionName = "barber";


call_user_func($dynamicFunctionName, 'mushroom');

where the dynamicFunctionName string could be more exciting and generated at run-time. You shouldn't use call_user_func unless you have to, because it is slower.

Always use the actual function name when you know it.

call_user_func is for calling functions whose name you don't know ahead of time but it is much less efficient since the program has to lookup the function at runtime.

I imagine it is useful for calling a function that you don't know the name of in advance... Something like:

switch($value):
{
case 7:
$func = 'run';
break;
default:
$func = 'stop';
break;
}


call_user_func($func, 'stuff');

Although you can call variable function names this way:

function printIt($str) { print($str); }


$funcname = 'printIt';
$funcname('Hello world!');

there are cases where you don't know how many arguments you're passing. Consider the following:

function someFunc() {
$args = func_get_args();
// do something
}


call_user_func_array('someFunc',array('one','two','three'));

It's also handy for calling static and object methods, respectively:

call_user_func(array('someClass','someFunc'),$arg);
call_user_func(array($myObj,'someFunc'),$arg);

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

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

If all your functions were in the same namespace, then it wouldn't be such an issue, as you could use something like this:

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

Edit: Following @Jannis saying that I'm wrong I did a little more testing, and wasn't having 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 the second method works for PHP 7 onwards, but not PHP 5.6.

There are no benefits to call it like that, the word user mean it is for multiple user, it is useful to create modification without editing in core engine.

it used by wordpress to call user function in plugins

<?php
/* main.php */


require("core.php");
require("my_plugin.php");


the_content(); // "Hello I live in Tasikmalaya"

...

<?php
/* core.php */


$listFunc = array();
$content = "Hello I live in @@@";


function add_filter($fName, $funct)
{
global $listFunc;
$listFunc[$fName] = $funct;
}


function apply_filter($funct, $content)
{
global $listFunc;
foreach ($listFunc as $key => $value)
{
if ($key == $funct and is_callable($listFunc[$key]))
{
$content = call_user_func($listFunc[$key], $content);
}
}
echo $content;
}


function the_content()
{
global $content;
$content = apply_filter('the_content', $content);
echo $content;
}

....

<?php
/* my_plugin.php */


function changeMyLocation($content){
return str_replace('@@@', 'Tasikmalaya', $content);
}


add_filter('the_content', 'changeMyLocation');

With PHP 7 you can use the nicer variable-function syntax everywhere. It works with static/instance functions, and it can take an array of parameters. More info at https://trowski.com/2015/06/20/php-callable-paradox

$ret = $callable(...$params);