(PHP 4, PHP 5, PHP 7)
call_user_func — 锟窖碉拷一锟斤拷锟斤拷锟斤拷锟斤拷为锟截碉拷锟斤拷锟斤拷锟斤拷锟斤拷
锟斤拷一锟斤拷锟斤拷锟斤拷 callback
锟角憋拷锟斤拷锟矫的回碉拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷腔氐锟斤拷锟斤拷锟斤拷牟锟斤拷锟斤拷锟�
callback
锟斤拷锟斤拷锟斤拷锟矫的回碉拷锟斤拷锟斤拷锟斤拷callable锟斤拷锟斤拷
parameter
0锟斤拷锟斤拷锟斤拷锟较的诧拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷氐锟斤拷锟斤拷锟斤拷锟�
Note:
锟斤拷注锟解,锟斤拷锟斤拷call_user_func()锟侥诧拷锟斤拷锟斤拷锟斤拷为锟斤拷锟矫达拷锟捷★拷
Example #1 call_user_func() 锟侥参匡拷锟斤拷锟斤拷
<?php
error_reporting(E_ALL);
function increment(&$var)
{
$var++;
}
$a = 0;
call_user_func('increment', $a);
echo $a."\n";
call_user_func_array('increment', array(&$a)); // You can use this instead before PHP 5.3
echo $a."\n";
?>锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�
0 1
锟斤拷锟截回碉拷锟斤拷锟斤拷锟侥凤拷锟斤拷值锟斤拷
锟芥本 | 说锟斤拷 |
---|---|
5.3.0 |
锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟侥关硷拷锟街的斤拷锟斤拷锟斤拷锟斤拷锟斤拷强锟斤拷锟节达拷之前锟斤拷使锟斤拷锟斤拷锟斤拷冒锟斤拷锟斤拷锟斤拷锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷为锟斤拷锟斤拷锟斤拷锟斤拷为锟截碉拷锟斤拷锟斤拷锟侥伙拷锟斤拷锟斤拷锟结发锟斤拷一锟斤拷E_STRICT 锟侥撅拷锟芥,锟斤拷为锟斤拷锟斤拷锟斤拷锟侥诧拷锟斤拷锟斤拷锟斤拷为锟斤拷态锟斤拷锟斤拷锟斤拷
|
Example #2 call_user_func() 锟斤拷锟斤拷锟斤拷
<?php
function barber($type)
{
echo "You wanted a $type haircut, no problem\n";
}
call_user_func('barber', "mushroom");
call_user_func('barber', "shave");
?>
锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�
You wanted a mushroom haircut, no problem You wanted a shave haircut, no problem
Example #3 call_user_func() 锟斤拷锟斤拷锟秸硷拷锟绞癸拷锟�
<?php
namespace Foobar;
class Foo {
static public function test() {
print "Hello world!\n";
}
}
call_user_func(__NAMESPACE__ .'\Foo::test'); // As of PHP 5.3.0
call_user_func(array(__NAMESPACE__ .'\Foo', 'test')); // As of PHP 5.3.0
?>
锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�
Hello world! Hello world!
Example #4 锟斤拷call_user_func()锟斤拷锟斤拷锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷姆锟斤拷锟�
<?php
class myclass {
static function say_hello()
{
echo "Hello!\n";
}
}
$classname = "myclass";
call_user_func(array($classname, 'say_hello'));
call_user_func($classname .'::say_hello'); // As of 5.2.3
$myobject = new myclass();
call_user_func(array($myobject, 'say_hello'));
?>
锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�
Hello! Hello! Hello!
Example #5 锟斤拷锟斤拷锟斤拷锟侥猴拷锟斤拷锟斤拷为锟截碉拷锟斤拷锟斤拷call_user_func()
<?php
call_user_func(function($arg) { print "[$arg]\n"; }, 'test'); /* As of PHP 5.3.0 */
?>
锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�
[test]
Note:
锟节猴拷锟斤拷锟斤拷注锟斤拷锟叫讹拷锟斤拷氐锟斤拷锟斤拷锟绞�(锟斤拷使锟斤拷 call_user_func() 锟斤拷 call_user_func_array())锟斤拷锟斤拷锟斤拷前一锟斤拷锟截碉拷锟斤拷锟斤拷未锟斤拷锟斤拷锟斤拷斐o拷锟斤拷锟斤拷慕锟斤拷锟斤拷俦锟斤拷锟斤拷谩锟�