如何用代码说明PHP动态调用函数
Admin 2022-10-09 群英技术资讯 689 次浏览
关于“如何用代码说明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安全攻防如何利用文件上传漏洞与绕过技巧详解,有需要的朋友可以参考借鉴下,希望大家多多进步早日升职加薪
这篇文章主要介绍了ThinkPHP5+jQuery+MySql实现投票功能,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
今天小编就为大家分享一篇Thinkphp页面跳转设置跳转等待时间的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
文本主要给大家分享的是关于PHP超级全局变量的内容,这篇主要介是几个常用PHP 超级全局变量,PHP $GLOBALS、PHP $_REQUEST、PHP $_POST和PHP $_GET,感兴趣的朋友就继续往下看吧。
在本篇文章里小编给大家整理了一篇关于php中的标量数据类型总结内容,对此有兴趣的朋友们可以跟着学习下。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008