call_user_func 和 call_user_func_array用法
说明
call_user_func 和 call_user_func_array
相同:都可以调用函数和类内部的函数,
不同:不同的是传递的参数不同,前者是一个参数一个参数传递, 后者是传递array参数
一:call_user_func 用法
1.1 调用函数
<?php
function funcA($a) {
echo $a;
} function funcB() {
$args = func_get_args();
print_r($args);
} call_user_func('funcA', 'testA');
call_user_func('funcB', 'testA', 'testB');
1.2 类内部函数调用
class clsA {
// func_get_args() 函数获取参数
public function funcA() {
$args = func_get_args();
$num = func_num_args();
print_r($args);
echo $num;
}
public static function funcB() {
$args = func_get_args();
$num = func_num_args();
print_r($args);
echo $num;
}
public static function funcC($a, $b) {
echo $a.' - ' . $b;
}
}
call_user_func(array('clsA', 'funcA'), 'arg1', 'arg2');
call_user_func('clsA::funcB', 'argb1', ' argb2');
call_user_func('clsA::funcC', 'argc1', 'argc2');
二:call_user_func_array 用法
从上面的例子可以看到 func_get_args() 可以获取函数的参数,而且是数组形式的参数,那么能不能直接传给函数呢,对,就是这个 call_user_func_array
2.1 调用函数
<?php
//直接传递参数
function funcC($a, $b) {
echo $a .' - '. $b;
} //用 func_get_args() 函数获取传递的参数
function funcD($a, $b) {
$args = func_get_args();
print_r($args);
}
//后面传递的是array参数
call_user_func_array('funcC', array('1', '2'));
call_user_func_array('funcD', array('d1', 'd2'));
2.2 类内部函数调用
class funcE {
//直接获取参数
static function funcA($a, $b) {
echo " " .$a . ' - ' . $b;
}
//用func_get_args()获取参数
static function funcB() {
$args = func_get_args();
print_r($args);
}
}
// 2种调用方法
//这样调用
call_user_func_array(array('funcE', 'funcA'), array('1', '2'));
call_user_func_array(array('funcE', 'funcB'), array('1', '2', 3));
//或者这样调用
call_user_func_array('funcE::funcA', array('1', '2'));
call_user_func_array('funcE::funcB', array('1', '2', 3));
call_user_func 和 call_user_func_array用法的更多相关文章
- PHP函数call_user_func和call_user_func_array详解
今天在群里面,有个叫lewis的在问call_user_func_array的用法,因为之前一直没有用过,也不能说什么,于是看一下手册,发现是这么写的: call_user_func_array (P ...
- PHP回调函数call_user_func()和call_user_func_array()的使用
call_user_func():把第一个参数作为回调函数调用 用法:call_user_func ( callable $callback [, mixed $parameter [, mixed ...
- php中call_user_func 与 call_user_func_array的使用
call_user_func()是利用回调函数处理字符串,call_user_func_array是利用回调函数处理数组. // 1. 调用自定义函数 function test($a, $b) { ...
- php自定义函数call_user_func和call_user_func_array详解
看UCenter的时候有一个函数call_user_func,百思不得其解,因为我以为是自己定义的函数,结果到处都找不到,后来百度了一下才知道call_user_func是内置函 call_user_ ...
- php call_user_func和call_user_func_array
首先要看这个页面关于callable类型:http://www.php.net/manual/zh/language.types.callable.php 自 PHP 5.4 起可用 callable ...
- PHP中的call_user_func()与call_user_func_array()简单理解
原文地址:http://small.aiweimeng.top/index.php/archives/52.html call_user_func:把一个参数作为回调函数调用 用法说明: call_u ...
- php中call_user_func()与call_user_func_array()区别
call_user_func:把一个参数作为回调函数调用 用法说明: call_user_func ( callable $callback [, mixed $parameter [, mixed ...
- 简单理解call_user_func和call_user_func_array两个函数
call_user_func():调用一个回调函数处理字符串, 可以用匿名函数,可以用有名函数,可以传递类的方法, 用有名函数时,只需传函数的名称 用类的方法时,要传类的名称和方法名 传递的第一个参数 ...
- call_user_func 与call_user_func_array 的使用与区别
1 call_user_func 的使用 1)使用方法直接传递值 function nowamagic($a,$b){ echo $a; echo $b; } call_user_func('nowa ...
随机推荐
- C#【结对编程作业】小学数学习题助手
一.软件成品展示 软件本体下载(包括程序及其更新日志,源码工程包,UML图,API接口文档,算法介绍文档,算式计算excel实例,浅查重程序) 链接: http://pan.baidu.com/s/1 ...
- Linux内核及分析 第一周 计算机是如何工作的?
C语言代码: int g(int x) { return x + 5; } int f(int x) { return g(x); } int main(void) { return f(5) + 1 ...
- debug网页时小问题The source attachment does not contain the source for the file
第一次debug总是出现下图问题,提示我没加源码... The source attachment does not contain the source for the file ... 解决方法: ...
- Neo4j学习案例【转】
转自 打怪的蚂蚁 CSDN: https://blog.csdn.net/xgjianstart/article/details/77285334 neo4j有社区版本和企业版.社区版本是免费的,只支 ...
- Python表达式与运算符
表达式与运算符 Python语言支持以下类型的运算符: 算术运算符 比较(关系)运算符 赋值运算符 逻辑运算符 位运算符 成员运算符 身份运算符 运算符优先级 算术运算符 运算符 描述 + 加 - 两 ...
- SAP PA认证
----------------------------------------------------------------转帖---------------------------------- ...
- appium学习记录2
unittest 学习 每执行一次 testcase 就会调用一次 setUP 与teardown 类方法只会执行一次 开始 与结束时候执行 类似反射方法 __init__ 与 __del__ set ...
- hdu6165(拓扑排序+tarjan缩点)
题意:就任意两个点能否到达: 解题思路:首先将图简化,比如假设图里有一个环,那么,这环内两个点肯定是能相互到达的,那么就不用考虑这环内的点了,很简单就想到用tarjan算法将环缩成一个点,然后就是判断 ...
- Js更改样式导致hover效果消失
[问题来源] 今天做单次倒计时,利用JS更改了button样式之后,再次点击时,发现hover效果消失. 原因: CSS的优先级问题导致 [解决方法] 利用!important提高hover的优先级 ...
- MT【49】四次函数求最值
已知$f(x)=(1-x^2)(x^2+ax+b)$的图像关于x=3对称,求$f(x)$的最大值. 解答:显然$-1,7;1,5$是$f(x)=0$的根.故$(x^2+ax+b)=(x-5)(x-7) ...