PHP处理函数的函数(Function handling Function)
直接上代码
/** 参考 http://php.net/manual/zh/function.call-user-func-array.php PHP中处理函数的函数(Function Handling Functions) call_user_func_array — 让参数以数组的形式调用一个函数 call_user_func — 调用一个存在的函数 create_function — 建立一个函数 func_get_arg — 获取函数中某个参数的值 func_get_args — 获取函数的所有参数并组成数组 func_num_args — 获取一个函数的参数个数 function_exists — 判定一个函数是否存在 get_defined_functions — 获取已有的函数信息 register_shutdown_function — 注册一个页面载入完成后运行的函数 register_tick_function — 注册一个按要求调用的函数 unregister_tick_function — 取消一个按要求调用的函数 get_defined_functions可以获取所有的PHP函数和自定义的函数: **/ /** mixed ¶ mixed 说明一个参数可以接受多种不同的(但不一定是所有的)类型。 例如 gettype() 可以接受所有的 PHP 类型,str_replace() 可以接受字符串和数组。 number ¶ number 说明一个参数可以是 integer 或者 float。 callback ¶ 本文档中在 PHP 5.4 引入 callable 类型之前使用 了 callback 伪类型。二者含义完全相同。 void ¶ void 作为返回类型意味着函数的返回值是无用的。void 作为参数列表意味着函数不接受任何参数。 **/ /** 调用回调函数,并把一个数组参数作为回调函数的参数 调用方法 mixed call_user_func_array ( callable $callback , array $param_arr ) 返回值 返回回调函数的结果。如果出错的话就返回FALSE **/ function foobar($arg, $arg2) { echo __FUNCTION__, " 包含参数 $arg 和 $arg2\n"; } call_user_func_array("foobar", array("111", "222", "333")); //foobar 函数 部分参数 111 和 222 exit; class foo { public function bar($arg, $arg2) { echo __METHOD__, "方法 部分参数 $arg and $arg2<br />"; } static function a($arg, $arg2) { echo __METHOD__, "方法 部分参数 $arg and $arg2<br />"; } } $foo = new foo; call_user_func_array(array($foo, "bar"), array("111", "222","333")); //静态调用方式 call_user_func_array(array("foo", "a"), array("111", "222","333")); /**foo::bar方法 部分参数 111 and 222 foo::a方法 部分参数 111 and 222 **/ exit; namespace Foobar; class Foo { static public function test($name) { print "Hello {$name}!<br />"; } } // As of PHP 5.3.0 call_user_func_array(__NAMESPACE__ .'\Foo::test', array('111')); // As of PHP 5.3.0 call_user_func_array(array(__NAMESPACE__ .'\Foo', 'test'), array('222')); /** Hello 111! Hello 222! **/ exit; $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) exit; //引用 function a(&$b) { $b++; } $c = 0; call_user_func_array('a',array(&$c)); echo $c,'<br />'; call_user_func_array('a',array(&$c)); echo $c,'<br />'; /** 1 2 **/ exit; /** 注:call_user_func_array函数和call_user_func很相似,只不过是换了一种方式传递了参数,让参数的结构更清晰: 注:传入call_user_func()的参数不能为引用传递 就是 引用传递是不起作用的 把第一个参数作为回调函数调用 调用方法 mixed call_user_func ( callable $callback [, mixed $parameter [, mixed $... ]] ) 返回值 返回回调函数的返回值,如果错误则返回FALSE。 **/ function a(&$b) { $b++; } $c = 0; call_user_func('a',array(&$c)); echo $c,'<br />'; call_user_func('a',array(&$c)); echo $c,'<br />'; /** 0 0 **/ exit; function a($b,$c) { print_r($b); echo "<br/>"; echo $c."<br/>"; } call_user_func('a', ["111","222"],"333"); //多类型多个参数 /** Array ( [0] => 111 [1] => 222 ) 333 **/ exit; call_user_func(function ($b,$c) { print_r($b); echo "<br/>"; echo $c."<br/>"; } , ["111","222"],"333"); /** Array ( [0] => 111 [1] => 222 ) 333 **/ exit; //基本调用与 call_user_func_array 类似 /** 函数可以建立一个匿名的函数(函数名被PHP默认为lambda_1,lambda_2),样子比较古怪,但是形式比较奇特,要注意第二个参数内的语句要有“;”分隔: 调用方法 string create_function ( string $args , string $code ) 返回值 返回唯一的函数名字符串,如果错误则返回FALSE。 **/ $newfunc = create_function('$a,$b', 'return $a + $b;'); echo $newfunc."<br />"; echo $newfunc(2, 3)."<br />"; /** lambda_1 5 **/ exit; $sv = array("small", "larger", "a big string", "it is a string thing"); 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 ) **/ exit; $av = array("the ", "a ", "that ", "this "); array_walk($av, create_function('&$v,$k', '$v = $v . "mango";'));//array_walk(array,myfunction,userdata...) print_r($av); /** Array ( [0] => the mango [1] => a mango [2] => that mango [3] => this mango ) **/ exit; function process($var1, $var2, $farr) { foreach ($farr as $f) { echo $f($var1, $var2) . "<br />"; } } // 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 "<br/>Using the first array of anonymous functions<br/>"; echo "parameters: 2.3445, M_PI<br/>"; echo M_PI."<br />"; 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 3.1415926535898 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: 3569586014, 342550513 similar(a,b) = 11(45.833333333333%) **/ exit; /** 返回参数列表的某一项 调用方法 mixed func_get_arg ( int $arg_num ) 返回值 返回指定的参数,错误则返回 FALSE 错误/异常 当在自定义函数的外面调用的该函数的时候会发出一个警告, 或者是当 arg_num 比实际传入的参数的数目大的时候也会发出一个警告 **/ function foo() { echo func_get_arg(1); } foo (111, 222, 333); //显示 222 exit; /** func_get_args — 返回一个包含函数参数列表的数组 调用方法 array func_get_args ( void ) 返回值 返回一个数组,其中每个元素都是目前用户自定义函数的参数列表的相应元素的副本 **/ $bb = 3434; function a1() { $numargs = func_num_args(); $b = func_get_args(); print_r($b); } a1(111, 222, 333); //print_r(func_get_args()); xxxx /** Array ( [0] => 111 [1] => 222 [2] => 333 ) Warning: func_get_args(): Called from the global scope - no function context in E:\WWW\test\test.php on line 11 **/ /** func_num_args函数可以获取函数接受到参数的数量 调用 int func_num_args ( void ) 返回 Returns the number of arguments passed into the current user-defined function. **/ function a() { echo func_num_args(); } a(111, 222, 333); //显示 3 /** function_exists **/ /** get_defined_functions — 获取已有的函数信息 **/ /** register_shutdown_function — 注册一个页面载入完成后运行的函数 void register_shutdown_function ( callable $callback [, mixed $parameter [, mixed $... ]] ) 没有返回值。 **/ function a1() { echo 222 ; } register_shutdown_function('a1'); echo 111; //显示 111 222 exit; /** bool register_tick_function ( callable $function [, mixed $arg [, mixed $... ]] ) 成功时返回 TRUE, 或者在失败时返回 FALSE。 **/ //Tick 是一个在 declare 代码段中解释器每执行 N 条低级语句就会发生的事件。N 的值是在 declare 中的 directive 部分用 ticks=N 来指定的。在每个 tick 中出现的事件是由 register_tick_function() 来指定的 function foo($str,$str2) { static $i = 0; print "$str$str2: $i<br>"; $i++; } register_tick_function("foo", "count","hahahah"); declare (ticks = 6) { for($i=0; $i<20; $i++) { echo "$i<br>"; } } //void unregister_tick_function ( string $function_name ) /** 0 1 2 3 4 5 counthahahah: 0 6 7 8 9 10 11 counthahahah: 1 12 13 14 15 16 17 counthahahah: 2 18 19 **/ exit;
浙公网安备 33010602011771号