call_user_func_array

(PHP 4 >= 4.0.4, PHP 5, PHP 7)

call_user_func_array锟斤拷锟矫回碉拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟轿拷氐锟斤拷锟斤拷锟斤拷牟锟斤拷锟�

说锟斤拷

call_user_func_array ( callable $callback , array $param_arr ) : mixed

锟窖碉拷一锟斤拷锟斤拷锟斤拷锟斤拷为锟截碉拷锟斤拷锟斤拷锟斤拷callback锟斤拷锟斤拷锟矫o拷锟窖诧拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷param_arr锟斤拷为锟截碉拷锟斤拷锟斤拷锟侥的诧拷锟斤拷锟斤拷锟诫。

锟斤拷锟斤拷

callback

锟斤拷锟斤拷锟矫的回碉拷锟斤拷锟斤拷锟斤拷

param_arr

要锟斤拷锟斤拷锟斤拷氐锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷椋拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷椤�

锟斤拷锟斤拷值

锟斤拷锟截回碉拷锟斤拷锟斤拷锟侥斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷幕锟斤拷头锟斤拷锟�FALSE

锟斤拷锟斤拷锟斤拷志

锟芥本 说锟斤拷
5.3.0 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟侥关硷拷锟街的斤拷锟斤拷锟斤拷锟斤拷锟斤拷强锟斤拷锟节达拷之前锟斤拷使锟斤拷锟斤拷锟斤拷冒锟斤拷锟斤拷锟斤拷锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷为锟斤拷锟斤拷锟斤拷锟斤拷为锟截碉拷锟斤拷锟斤拷锟侥伙拷锟斤拷锟斤拷锟结发锟斤拷一锟斤拷E_STRICT锟侥撅拷锟芥,锟斤拷为锟斤拷锟斤拷锟斤拷锟侥诧拷锟斤拷锟斤拷锟斤拷为锟斤拷态锟斤拷锟斤拷锟斤拷

锟斤拷锟斤拷

Example #1 call_user_func_array()锟斤拷锟斤拷

<?php
function foobar($arg$arg2) {
    echo 
__FUNCTION__" got $arg and $arg2\n";
}
class 
foo {
    function 
bar($arg$arg2) {
        echo 
__METHOD__" got $arg and $arg2\n";
    }
}


// Call the foobar() function with 2 arguments
call_user_func_array("foobar", array("one""two"));

// Call the $foo->bar() method with 2 arguments
$foo = new foo;
call_user_func_array(array($foo"bar"), array("three""four"));
?>

锟斤拷锟斤拷锟斤拷锟教碉拷锟斤拷锟斤拷锟斤拷锟斤拷冢锟�

foobar got one and two
foo::bar got three and four

Example #2 call_user_func_array()使锟斤拷锟斤拷锟斤拷锟秸硷拷锟斤拷锟斤拷

<?php

namespace Foobar;

class 
Foo {
    static public function 
test($name) {
        print 
"Hello {$name}!\n";
    }
}

// As of PHP 5.3.0
call_user_func_array(__NAMESPACE__ .'\Foo::test', array('Hannes'));

// As of PHP 5.3.0
call_user_func_array(array(__NAMESPACE__ .'\Foo''test'), array('Philip'));

?>

锟斤拷锟斤拷锟斤拷锟教碉拷锟斤拷锟斤拷锟斤拷锟斤拷冢锟�

Hello Hannes!
Hello Philip!

Example #3 锟斤拷锟斤拷锟斤拷锟侥猴拷锟斤拷锟斤拷为锟截碉拷锟斤拷锟斤拷call_user_func_array()

<?php

$func 
= function($arg1$arg2) {
    return 
$arg1 $arg2;
};

var_dump(call_user_func_array($func, array(24))); /* As of PHP 5.3.0 */

?>

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

int(8)

Example #4 锟斤拷锟斤拷锟斤拷

<?php

function mega(&$a){
    
$a 55;
    echo 
"function mega \$a=$a\n";
}
$bar 77;
call_user_func_array('mega',array(&$bar));
echo 
"global \$bar=$bar\n";

?>

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

function mega $a=55
global $bar=55

注锟斤拷

Note:

PHP 5.4之前锟斤拷锟斤拷锟�param_arr锟斤拷锟斤拷牟锟斤拷锟斤拷锟斤拷锟斤拷么锟街碉拷锟斤拷锟矫达拷锟斤拷锟皆拷锟斤拷锟侥拷系母锟斤拷锟斤拷锟斤拷锟斤拷遣锟斤拷锟斤拷锟斤拷么锟街碉拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷梅锟绞斤拷锟斤拷氲斤拷氐锟斤拷锟斤拷锟斤拷锟斤拷锟饺伙拷锟斤拷锟斤拷么锟街碉拷锟斤拷址锟绞斤拷锟斤拷锟斤拷莶锟斤拷锟斤拷锟斤拷氐锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷岱拷锟斤拷锟街э拷值木锟斤拷妫拷锟斤拷遣锟斤拷锟斤拷锟矫此碉拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷遣锟斤拷锟街э拷值摹锟斤拷锟斤拷锟斤拷锟絇HP 5.4锟斤拷锟芥被去锟斤拷锟剿★拷锟斤拷锟揭o拷锟斤拷也锟斤拷锟斤拷锟斤拷锟斤拷锟节诧拷锟斤拷锟斤拷锟斤拷for which the function signature is honored锟斤拷锟斤拷锟斤拷氐锟斤拷锟斤拷锟侥拷锟斤拷锟斤拷锟斤拷锟揭拷锟斤拷艿牟锟斤拷锟斤拷锟斤拷锟斤拷么锟斤拷莸锟绞憋拷颍锟街碉拷锟斤拷荩锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟揭伙拷锟斤拷锟斤拷妗�call_user_func() 锟斤拷锟结返锟斤拷 FALSE锟斤拷there is, however, an exception for passed values with reference count = 1, such as in literals, as these can be turned into references without ill effects — but also without writes to that value having any effect —; do not rely in this behavior, though, as the reference count is an implementation detail and the soundness of this behavior is questionable锟斤拷锟斤拷

Note:

锟节猴拷锟斤拷锟斤拷注锟斤拷锟叫讹拷锟斤拷氐锟斤拷锟斤拷锟绞�(锟斤拷使锟斤拷 call_user_func() 锟斤拷 call_user_func_array())锟斤拷锟斤拷锟斤拷前一锟斤拷锟截碉拷锟斤拷锟斤拷未锟斤拷锟斤拷锟斤拷斐o拷锟斤拷锟斤拷慕锟斤拷锟斤拷俦锟斤拷锟斤拷谩锟�

锟轿硷拷