如何用代码说明PHP动态调用函数
Admin 2022-10-09 群英技术资讯 619 次浏览
(PHP 4, PHP 5)
call_user_func — 传入一个参数来调用回调函数
$callback
[, mixed
$parameter
[, mixed
$...
]] )
Calls the callback
given by the first parameter and passes the remaining parameters as arguments.
callback
parameter
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]
(PHP 4 >= 4.0.4, PHP 5)
call_user_func_array — 传入一个参数数组来调用回调函数
$callback
, array
$param_arr
)
Calls the callback
given by the first parameter with the parameters in param_arr
.
callback
param_arr
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)
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
备忘录模式是一种行为型模式,它在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样可以在以后把该对象的状态恢复到之前保存的状态。主...
由于PHP的文件系统操作是基于C语言的函数的,所以它可能会以您意想不到的方式处理Null字符。Null字符在C语言中用于标识字符串结束,一个完整的字符串是从其开头到...
在本篇文章里小编给大家整理了一篇关于php去掉json反斜杠的实例讲解,有兴趣的朋友们可以参考学习下。
首次安装phpstorm是运行不了PHP文件的,因为环境没配置好。下面我将以PHPstorm和wampserver来配置PHP运行环境。
PHP内部函数的介绍:1、在函数内部声明的函数就是内部函数。2、使用注意,调用外部函数之后,就可以任意调用内部函数。在函数内部可以调用内部函数, 但是必须在内部函数定义之后调用。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008