如何用代码说明PHP动态调用函数
Admin 2022-10-09 群英技术资讯 1026 次浏览
关于“如何用代码说明PHP动态调用函数”的知识有一些人不是很理解,对此小编给大家总结了相关内容,具有一定的参考借鉴价值,而且易于学习与理解,希望能对大家有所帮助,有这个方面学习需要的朋友就继续往下看吧。(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正则表达式是一种描述字符串结构的语法规则,是一个特点的格式化模式,能够匹配,替换和街区匹配的字符串。一般PHP正则表达式会用作判断手机号码写法和邮箱写法是否正确,这篇文章也就给大家分享PHP的手机号码和邮箱的正则表达式实例。
PHP中runkit扩展的使用有哪些知识点?一些朋友可能会遇到这方面的问题,对此在下文小编向大家来讲解一下,内容详细,易于理解,希望大家阅读完这篇能有收获哦,有需要的朋友就往下看吧!
php中怎样检查文件是否存在?一些新手对于这个问题不是很清楚,下面就给大家介绍一下PHP检查文件是否存在方法,以及文件不存在要如何创建文件的操作,感兴趣的朋友可以了解一下。
本篇文章小编给大家分享一下python获取星期字符串代码实例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
php中的标量数据类型总结 PHP 的数据类型可以分为三大类,分别是标量数据类型.复合数据类型和特殊数据类型. 其中,标量数据类型是数据结构的最基础单元,只能存储一个数据.在 PHP 中的标量数据类型分为四种,如下表所示: 类型 功能 boolean(布尔型) 最简单的数据类型,只有两个值:true(真) / false(假) string(字符串) 字符串是连续的字符序列 integer(整型) 整型包含所有的整数,可以是正数也可以是负数 float(浮点型) 浮点型也是用来表示数字的,与整型不同除了可以表示整数外它还可
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008