教你如何用代码说明PHP动态调用函数
发布时间:2022-11-14 09:06 所属栏目:121 来源:互联网
导读:关于如何用代码说明PHP动态调用函数的知识有一些人不是很理解,对此小编给大家总结了相关内容,具有一定的参考借鉴价值,而且易于学习与理解,希望能对大家有所帮助,有这个方面学习需要的朋友就继续往下看吧。 call_user_func (PHP 4, PHP 5) call_user_fun
关于“如何用代码说明PHP动态调用函数”的知识有一些人不是很理解,对此小编给大家总结了相关内容,具有一定的参考借鉴价值,而且易于学习与理解,希望能对大家有所帮助,有这个方面学习需要的朋友就继续往下看吧。 call_user_func (PHP 4, PHP 5) call_user_func — 传入一个参数来调用回调函数 说明 mixed call_user_func ( callable $callback [, mixed $parameter [, mixed $... ]] ) Calls the callback given by the first parameter and passes the remaining parameters as arguments. 参数 callback The callable to be called. parameter Zero or more parameters to be passed to the callback. Note: Note that the parameters for call_user_func() are not passed by reference. Example #1 call_user_func() example and references <?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 返回值 Returns the return value of the callback, or FALSE on error. 范例 Example #2 call_user_func() example <?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() using namespace name <?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 Using a class method with 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 Using lambda function with call_user_func() <?php call_user_func(function($arg) { print "[$arg]\n"; }, 'test'); /* As of PHP 5.3.0 */ ?> 以上例程会输出: [test] call_user_func_array (PHP 4 >= 4.0.4, PHP 5) call_user_func_array — 传入一个参数数组来调用回调函数 说明 mixed call_user_func_array ( callable $callback , array $param_arr ) Calls the callback given by the first parameter with the parameters in param_arr. 参数 callback The callable to be called. param_arr The parameters to be passed to the callback, as an indexed array. 返回值 Returns the return value of the callback, or FALSE on error. 范例 Example #1 call_user_func_array() example <?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() using namespace name <?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 Using lambda function <?php $func = function($arg1, $arg2) { return $arg1 * $arg2; }; var_dump(call_user_func_array($func, array(2, 4))); /* As of PHP 5.3.0 */ ?> 以上例程会输出: int(8) “如何用代码说明PHP动态调用函数”的内容就介绍到这里了,感谢大家的阅读。 (编辑:ASP站长网) |
相关内容
网友评论
推荐文章
热点阅读