PHP函数处理方法总结
call_user_func_array
(PHP 4 >= 4.0.4, PHP 5, PHP 7)
call_user_func_array — 调用回调函数,并把一个数组参数作为回调函数的参数
说明
mixed call_user_func_array ( callable $callback , array $param_arr )
把第一个参数作为回调函数(callback)调用,把参数数组作(param_arr)为回调函数的的参数传入。
参数
callback
被调用的回调函数。
param_arr
要被传入回调函数的数组,这个数组得是索引数组。
返回值
返回回调函数的结果。如果出错的话就返回FALSE
更新日志
| 版本 | 说明 |
|---|---|
| 5.3.0 | 对面向对象里面的关键字的解析有所增强。在此之前,使用两个冒号来连接一个类和里面的一个方法,把它作为参数来作为回调函数的话,将会发出一个E_STRICT的警告,因为这个传入的参数被视为静态方法。 |
范例
Example #1 call_user_func_array()例子
<?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()使用命名空间的情况
<?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 把完整的函数作为回调传入call_user_func_array()
<?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)
Example #4 传引用
<?php
function mega(&$a){
$a = 55;
echo "function mega \$a=$a\n";
}
$bar = 77;
call_user_func_array('mega',array(&$bar));
echo "global \$bar=$bar\n";
?>
以上例程会输出:
function mega $a=55
global $bar=55
call_user_func
(PHP 4, PHP 5, PHP 7)
call_user_func — 把第一个参数作为回调函数调用
说明
mixed call_user_func ( callable $callback [, mixed $parameter [, mixed $... ]] )
第一个参数 callback 是被调用的回调函数,其余参数是回调函数的参数。
参数
callback
将被调用的回调函数(callable)。
parameter
0个或以上的参数,被传入回调函数。
Note:
请注意,传入call_user_func()的参数不能为引用传递。
Example #1 call_user_func() 的参考例子
<?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
返回值
返回回调函数的返回值。
更新日志
| 版本 | 说明 |
|---|---|
| 5.3.0 | 对面向对象里面的关键字的解析有所增强。在此之前,使用两个冒号来连接一个类和里面的一个方法,把它作为参数来作为回调函数的话,将会发出一个E_STRICT的警告,因为这个传入的参数被视为静态方法。 |
范例
Example #2 call_user_func() 的例子
<?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() 命名空间的使用
<?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 用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 把完整的函数作为回调传入call_user_func()
<?php
call_user_func(function($arg) { print "[$arg]\n"; }, 'test'); /* As of PHP 5.3.0 */
?>
以上例程会输出:
[test]
注释
Note:
在函数中注册有多个回调内容时(如使用 call_user_func() 与 call_user_func_array()),如在前一个回调中有未捕获的异常,其后的将不再被调用。
create_function
(PHP 4 >= 4.0.1, PHP 5, PHP 7)
create_function — Create an anonymous (lambda-style) function
说明
string create_function ( string $args , string $code )
从传递的参数创建一个匿名函数,并返回一个唯一的名称。
警告
此函数内部执行eval(),因此与eval()具有相同的安全性问题。 此外,它具有不良的性能和内存使用特性。
如果您使用的是PHP 5.3.0或更新版本,则应使用本机匿名函数。
参数
通常这些参数将作为单引号分隔的字符串传递。 使用单引号字符串的原因是保护变量名不被解析,否则,如果使用双引号,则需要转义变量名,例如。\$阿瓦尔。
args
函数参数。
code
功能码。
返回值
以字符串形式返回唯一的函数名称,或者返回错误的FALSE。
范例
Example #1 Creating an anonymous function with create_function()
您可以使用此功能,(例如)从运行时收集的信息创建一个函数:
<?php
$newfunc = create_function('$a,$b', 'return "ln($a) + ln($b) = " . log($a * $b);');
echo "New anonymous function: $newfunc\n";
echo $newfunc(2, M_E) . "\n";
// outputs
// New anonymous function: lambda_1
// ln(2) + ln(2.718281828459) = 1.6931471805599
?>
或者,可能有一般的处理函数可以将一组操作应用于参数列表:
Example #2 Making a general processing function with create_function()
<?php
function process($var1, $var2, $farr)
{
foreach ($farr as $f) {
echo $f($var1, $var2) . "\n";
}
} // create a bunch of math functions
$f1 = 'if ($a >=0) {return "b*a^2 = ".$b*sqrt($a);} else {return false;}';
$f2 = "return \"min(b^2+a, a^2,b) = \".min(\$a*\$a+\$b,\$b*\$b+\$a);";
$f3 = 'if ($a > 0 && $b != 0) {return "ln(a)/b = ".log($a)/$b; } else { return false; }';
$farr = array(
create_function('$x,$y', 'return "some trig: ".(sin($x) + $x*cos($y));'),
create_function('$x,$y', 'return "a hypotenuse: ".sqrt($x*$x + $y*$y);'),
create_function('$a,$b', $f1),
create_function('$a,$b', $f2),
create_function('$a,$b', $f3)
); echo "\nUsing the first array of anonymous functions\n";
echo "parameters: 2.3445, M_PI\n";
process(2.3445, M_PI, $farr); // now make a bunch of string processing functions
$garr = array(
create_function('$b,$a', 'if (strncmp($a, $b, 3) == 0) return "** \"$a\" '.
'and \"$b\"\n** Look the same to me! (looking at the first 3 chars)";'),
create_function('$a,$b', '; return "CRCs: " . crc32($a) . ", ".crc32($b);'),
create_function('$a,$b', '; return "similar(a,b) = " . similar_text($a, $b, &$p) . "($p%)";')
);
echo "\nUsing the second array of anonymous functions\n";
process("Twas brilling and the slithy toves", "Twas the night", $garr);
?>
以上例程会输出:
Using the first array of anonymous functions
parameters: 2.3445, M_PI
some trig: -1.6291725057799
a hypotenuse: 3.9199852871011
b*a^2 = 4.8103313314525
min(b^2+a, a^2,b) = 8.6382729035898
ln(a)/b = 0.27122299212594 Using the second array of anonymous functions
** "Twas the night" and "Twas brilling and the slithy toves"
** Look the same to me! (looking at the first 3 chars)
CRCs: -725381282, 342550513
similar(a,b) = 11(45.833333333333%)
但是,对于lambda风格(匿名)函数来说,最常见的用法是创建回调函数,例如使用array_walk()或usort()
Example #3 Using anonymous functions as callback functions
<?php
$av = array("the ", "a ", "that ", "this ");
array_walk($av, create_function('&$v,$k', '$v = $v . "mango";'));
print_r($av);
?>
以上例程会输出:
Array
(
[0] => the mango
[1] => a mango
[2] => that mango
[3] => this mango
)
一串字符串从较短到较长的顺序排列
<?php
$sv = array("small", "larger", "a big string", "it is a string thing");
print_r($sv);
?>
以上例程会输出:
Array
(
[0] => small
[1] => larger
[2] => a big string
[3] => it is a string thing
)
将其从更长到更短的排序
<?php
usort($sv, create_function('$a,$b','return strlen($b) - strlen($a);'));
print_r($sv);
?>
以上例程会输出:
Array
(
[0] => it is a string thing
[1] => a big string
[2] => larger
[3] => small
)
forward_static_call_array
(PHP 5 >= 5.3.0, PHP 7)
forward_static_call_array — Call a static method and pass the arguments as array
说明
mixed forward_static_call_array ( callable $function , array $parameters )
....
....
....
给个目录
- call_user_func_array — 调用回调函数,并把一个数组参数作为回调函数的参数
- call_user_func — 把第一个参数作为回调函数调用
- create_function — Create an anonymous (lambda-style) function
- forward_static_call_array — Call a static method and pass the arguments as array
- forward_static_call — Call a static method
- func_get_arg — 返回参数列表的某一项
- func_get_args — 返回一个包含函数参数列表的数组
- func_num_args — Returns the number of arguments passed to the function
- function_exists — 如果给定的函数已经被定义就返回 TRUE
- get_defined_functions — 返回所有已定义函数的数组
- register_shutdown_function — 注册一个会在php中止时执行的函数
- register_tick_function — Register a function for execution on each tick
- unregister_tick_function — De-register a function for execution on each tick
PHP函数处理方法总结的更多相关文章
- Matlab中函数定义方法
Matlab自定义函数的六种方法 n1.函数文件+调用函数(命令)文件:需单独定义一个自定义函数的M文件: n2.函数文件+子函数:定义一个具有多个自定义函数的M文件: n3.Inline:无需M文件 ...
- Oracle数据库中调用Java类开发存储过程、函数的方法
Oracle数据库中调用Java类开发存储过程.函数的方法 时间:2014年12月24日 浏览:5538次 oracle数据库的开发非常灵活,不仅支持最基本的SQL,而且还提供了独有的PL/SQL, ...
- String类中的一些函数使用方法
最常用的就是Length()函数了,求字符串的长度 String s="";int i=s.length();i结果为0. 如果是String s=null;int i=s.len ...
- FastReport里面正确调用函数的方法
FastReport里面正确调用函数的方法 错误: [FormatDateTime('yyyy-mm-dd',[frxDBDataset1."日期"])] --------- ...
- asp.net中调用javascript自定义函数的方法(包括引入JavaScript文件)总结
通常javascript代码可以与HTML标签一起直接放在前 端页面中,但如果JS代码多的话一方面不利于维护,另一方面也对搜索引擎不友好,因为页面因此而变得臃肿:所以一般有良好开发习惯的程序员都会把 ...
- [xcode]Xcode查找函数(方法)调用及被调用
参考资料:http://stackoverflow.com/questions/7145045/find-method-references-in-xcode 这个功能有的说是 Find Caller ...
- 学习笔记:jquery1.9版本后废弃的函数和方法
jQuery1.9+ 废弃的函数和方法 升级Jquery版本遇到的问题 (转载自:http://www.ppblog.cn/jquery1-9live.html 版权归原作者所有) jQuery1. ...
- python基础:os模块中关于文件/目录常用的函数使用方法
Python是跨平台的语言,也即是说同样的源代码在不同的操作系统不需要修改就可以同样实现 因此Python的作者就倒腾了OS模块这么一个玩意儿出来,有了OS模块,我们不需要关心什么操作系统下使用什么模 ...
- php中调用用户自定义函数的方法:call_user_func,call_user_func_array
看UCenter的时候有一个函数call_user_func,百思不得其解,因为我以为是自己定义的函数,结果到处都找不到,后来百度了一下才知道call_user_func是内置函数,该函数允许用户调用 ...
- jquery常用函数与方法汇总
1.delay(duration,[queueName]) 设置一个延时来推迟执行队列中之后的项目. jQuery1.4新增.用于将队列中的函数延时执行.他既可以推迟动画队列的执行,也可以用于自定义队 ...
随机推荐
- JAVA实现随机无重复数字功能
本文给大家介绍如何在JAVA中实现随机无重复数字的功能.如果您是初学者的话,有必要看一看这篇文章,因为这个功能一般会在面试中遇到.包括我本人在招聘人员的时候也喜欢拿这个问题去问别人,主要看一看考虑问题 ...
- laravel安装 redis 并驱动 session
1)composer 安装 redis composer require predis/predis 如果感兴趣,可以看一下这里 2)配置 redis 连接(config/database.php 配 ...
- org.apache.hadoop.ipc.RemoteException(org.apache.hadoop.security.AccessControlException)
在运行hadoop的程序时,向hdfs中写文件时候,抛出异常信息如下: Caused by: org.apache.hadoop.ipc.RemoteException(org.apache.hado ...
- 第十篇:基于TCP的一对回射客户/服务器程序及其运行过程分析( 上 )
前言 本文将讲解一对经典的客户/服务器回射程序,感受网络编程的大致框架( 该程序稍作改装即可演变成各种提供其他服务的程序 ):同时,还将对其运行过程加以分析,观察程序背后协议的执行细节,学习调试网络程 ...
- js里面进行位运算时候的注意事项
&(按位与):两个操作数都是1就是1 任何数与1按位与的出来的都是其二进制最后一位:任何数与0按位与都是0|(按位或):两个操作数一个是1就是1 任何数与0按位或都是其本身^(按位异或):两个 ...
- 使用WdatePicker获取比当前时间大的写法
<input name="today.date" class="Wdate" type="text" onClick="Wd ...
- LeetCode——Single Number III
Description: Given an array of numbers nums, in which exactly two elements appear only once and all ...
- 2012Noip提高组Day1 T3 开车旅行
题目描述 小 A 和小 B 决定利用假期外出旅行,他们将想去的城市从 1 到 N 编号,且编号较小的 城市在编号较大的城市的西边,已知各个城市的海拔高度互不相同,记城市 i 的海拔高度为 Hi,城市 ...
- Weinre 远程调试移动端手机web页面
调试场景 1.调试页面在手机上.2.调试工具在PC的chrome3.手机跟pc要在同一个网络环境下,也就是都使用一个wifi 一.安装 Weinre 1.Weinre是基于nodejs实现的,所以使用 ...
- centos7关闭自动锁屏
centos 7默认几分钟不动就锁屏,实在很讨厌,所以在设置中将其去掉 1.不同的版本应该设置的地方稍有变化,我不敢保证我的这个方法你一定可以用,所以用之前希望你了解你的系统的版本. 2.左上角点击应 ...