函数处理 函数 业 ,精于勤 荒于嬉.

函数处理 函数 call_user_func_array 调用回调函数,并把一个数组参数作为回调函数的参数

发表日期:2021-07-01 08:57:15 | 来源: | 分类:函数处理 函数

      示例1
01<?php
02function foobar($arg$arg2) {
03    echo __FUNCTION__" got $arg and $arg2\n";
04}
05class foo {
06    function bar($arg$arg2) {
07        echo __METHOD__" got $arg and $arg2\n";
08    }
09}
10// Call the foobar() function with 2 argumentscall_user_func_array("foobar", array("one", "two"));
11// Call the $foo->bar() method with 2 arguments$foo = new foo;
12call_user_func_array(array($foo"bar"), array("three""four"));
13?>
      示例2
01<?php
02namespace Foobar;
03class Foo {
04    static public function test($name) {
05        print "Hello {
06$name}
07!\n";
08    }
09}
10// As of PHP 5.3.0call_user_func_array(__NAMESPACE__ .'\Foo::test', array('Hannes'));
11// As of PHP 5.3.0call_user_func_array(array(__NAMESPACE__ .'\Foo', 'test'), array('Philip'));
12?>
      示例3
1<?php
2$func function($arg1$arg2) {
3    return $arg1 $arg2;
4}
5;
6var_dump(call_user_func_array($funcarray(2, 4)));
7 /* As of PHP 5.3.0 */
8?>
      示例4
1<?php
2function mega(&$a){
3    $a = 55;
4    echo "function mega \$a=$a\n";
5}
6$bar = 77;
7call_user_func_array('mega',array(&$bar));
8echo "global \$bar=$bar\n";
9?>

阅读全文 »

函数处理 函数 call_user_func 把第一个参数作为回调函数调用

发表日期:2021-07-01 08:57:16 | 来源: | 分类:函数处理 函数

      示例1
01<?php
02error_reporting(E_ALL);
03function increment(&$var){
04    $var++;
05}
06$a = 0;
07call_user_func('increment'$a);
08echo $a."\n";
09call_user_func_array('increment'array(&$a));
10 // You can use this instead before PHP 5.3echo $a."\n";
11?>
      示例2
1<?php
2function barber($type){
3    echo "You wanted a $type haircut, no problem\n";
4}
5call_user_func('barber'"mushroom");
6call_user_func('barber'"shave");
7?>
      示例3
01<?php
02namespace Foobar;
03class Foo {
04    static public function test() {
05        print "Hello world!\n";
06    }
07}
08call_user_func(__NAMESPACE__ .'\Foo::test');
09 // As of PHP 5.3.0call_user_func(array(__NAMESPACE__ .'\Foo', 'test'));
10 // As of PHP 5.3.0?>
      示例4
01<?php
02class myclass {
03    static function say_hello()    {
04        echo "Hello!\n";
05    }
06}
07$classname "myclass";
08call_user_func(array($classname'say_hello'));
09call_user_func($classname .'::say_hello');
10 // As of 5.2.3$myobject = new myclass();
11call_user_func(array($myobject'say_hello'));
12?>
      示例5
1<?php
2call_user_func(function($arg) {
3 print "[$arg]\n";
4 }
5'test');
6 /* As of PHP 5.3.0 */
7?>

阅读全文 »

函数处理 函数 create_function Create an anonymous (lambda-style) function

发表日期:2021-07-01 08:57:16 | 来源: | 分类:函数处理 函数

      示例1
1<?php
2$newfunc = create_function('$a,$b', 'return "ln($a) + ln($b) = " . log($a $b);
3');
4echo "New anonymous function: $newfunc\n";
5echo $newfunc(2, M_E) . "\n";
6// outputs// New anonymous function: lambda_1// ln(2) + ln(2.718281828459) = 1.6931471805599?>
      示例2
01<?php
02function process($var1$var2$farr){
03    foreach ($farr as $f) {
04        echo $f($var1$var2) . "\n";
05    }
06}
07// create a bunch of math functions$f1 = 'if ($a >=0) {
08return "b*a^2 = ".$b*sqrt($a);
09}
10 else {
11return false;
12}
13';
14$f2 = "return \"min(b^2+a, a^2,b) = \".min(\$a*\$a+\$b,\$b*\$b+\$a);
15";
16$f3 = 'if ($a > 0 && $b != 0) {
17return "ln(a)/b = ".log($a)/$b;
18 }
19 else {
20 return false;
21 }
22';
23$farr array(    create_function('$x,$y', 'return "some trig: ".(sin($x) + $x*cos($y));
24'),    create_function('$x,$y', 'return "a hypotenuse: ".sqrt($x*$x $y*$y);
25'),    create_function('$a,$b', $f1),    create_function('$a,$b', $f2),    create_function('$a,$b', $f3)    );
26echo "\nUsing the first array of anonymous functions\n";
27echo "parameters: 2.3445, M_PI\n";
28process(2.3445, M_PI, $farr);
29// 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)";
30'),    create_function('$a,$b', ';
31 return "CRCs: " . crc32($a) . ", ".crc32($b);
32'),    create_function('$a,$b', ';
33 return "similar(a,b) = " . similar_text($a$b, &$p) . "($p%)";
34')    );
35echo "\nUsing the second array of anonymous functions\n";
36process("Twas brilling and the slithy toves""Twas the night"$garr);
37?>
      示例3
1<?php
2$av array("the ""a ""that ""this ");
3array_walk($av, create_function('&$v,$k', '$v $v "mango";
4'));
5print_r($av);
6?>
      示例4
1<?php
2$sv array("small""larger""a big string""it is a string thing");
3print_r($sv);
4?>
      示例5
1<?php
2usort($sv, create_function('$a,$b','return strlen($b) - strlen($a);
3'));
4print_r($sv);
5?>

阅读全文 »

函数处理 函数 forward_static_call_array Call a static method and pass the arguments as array

发表日期:2021-07-01 08:57:15 | 来源: | 分类:函数处理 函数

      示例1
01<?php
02class A{
03    const NAME = 'A';
04    public static function test() {
05        $args = func_get_args();
06        echo static::NAME, " ".join(','$args)." \n";
07    }
08}
09class extends A{
10    const NAME = 'B';
11    public static function test() {
12        echo self::NAME, "\n";
13        forward_static_call_array(array('A''test'), array('more''args'));
14        forward_static_call_array( 'test'array('other''args'));
15    }
16}
17B::test('foo');
18function test() {
19        $args = func_get_args();
20        echo "C ".join(','$args)." \n";
21    }
22?>

阅读全文 »

函数处理 函数 forward_static_call Call a static method

发表日期:2021-07-01 08:57:15 | 来源: | 分类:函数处理 函数

      示例1
01<?php
02class A{
03    const NAME = 'A';
04    public static function test() {
05        $args = func_get_args();
06        echo static::NAME, " ".join(','$args)." \n";
07    }
08}
09class extends A{
10    const NAME = 'B';
11    public static function test() {
12        echo self::NAME, "\n";
13        forward_static_call(array('A''test'), 'more''args');
14        forward_static_call( 'test''other''args');
15    }
16}
17B::test('foo');
18function test() {
19        $args = func_get_args();
20        echo "C ".join(','$args)." \n";
21    }
22?>

阅读全文 »

函数处理 函数 func_get_arg 返回参数列表的某一项

发表日期:2021-07-01 08:57:15 | 来源: | 分类:函数处理 函数

      示例1
01<?php
02function foo(){
03     $numargs = func_num_args();
04     echo "Number of arguments: $numargs<br />\n";
05     if ($numargs >= 2) {
06         echo "Second argument is: " . func_get_arg(1) . "<br />\n";
07     }
08}
09foo (1, 2, 3);
10?>
      示例2
1test.php<?php
2function foo() {
3    include './fga.inc';
4}
5foo('First arg''Second arg');
6?>fga.inc<?php$arg = func_get_arg(1);
7var_export($arg);
8?>
      示例3
01<?php
02function byVal($arg) {
03    echo 'As passed     : ', var_export(func_get_arg(0)), PHP_EOL;
04    $arg 'baz';
05    echo 'After change  : ', var_export(func_get_arg(0)), PHP_EOL;
06}
07function byRef(&$arg) {
08    echo 'As passed     : ', var_export(func_get_arg(0)), PHP_EOL;
09    $arg 'baz';
10    echo 'After change  : ', var_export(func_get_arg(0)), PHP_EOL;
11}
12$arg 'bar';
13byVal($arg);
14byRef($arg);
15?>

阅读全文 »

函数处理 函数 func_get_args 返回一个包含函数参数列表的数组

发表日期:2021-07-01 08:57:16 | 来源: | 分类:函数处理 函数

      示例1
01<?php
02function foo(){
03    $numargs = func_num_args();
04    echo "Number of arguments: $numargs<br />\n";
05    if ($numargs >= 2) {
06        echo "Second argument is: " . func_get_arg(1) . "<br />\n";
07    }
08    $arg_list = func_get_args();
09    for ($i = 0;
10 $i $numargs;
11 $i++) {
12        echo "Argument $i is: " $arg_list[$i] . "<br />\n";
13    }
14}
15foo(1, 2, 3);
16?>
      示例2
1test.php<?php
2function foo() {
3    include './fga.inc';
4}
5foo('First arg''Second arg');
6?>fga.inc<?php$args = func_get_args();
7var_export($args);
8?>
      示例3
01<?php
02function byVal($arg) {
03    echo 'As passed     : ', var_export(func_get_args()), PHP_EOL;
04    $arg 'baz';
05    echo 'After change  : ', var_export(func_get_args()), PHP_EOL;
06}
07function byRef(&$arg) {
08    echo 'As passed     : ', var_export(func_get_args()), PHP_EOL;
09    $arg 'baz';
10    echo 'After change  : ', var_export(func_get_args()), PHP_EOL;
11}
12$arg 'bar';
13byVal($arg);
14byRef($arg);
15?>

阅读全文 »

函数处理 函数 func_num_args Returns the number of arguments passed to the function

发表日期:2021-07-01 08:57:16 | 来源: | 分类:函数处理 函数

      示例1
1<?php
2function foo(){
3    $numargs = func_num_args();
4    echo "Number of arguments: $numargs\n";
5}
6foo(1, 2, 3);
7   ?>
      示例2
1test.php<?php
2function foo() {
3    include './fna.php';
4}
5foo('First arg''Second arg');
6?>fna.php<?php$num_args = func_num_args();
7var_export($num_args);
8?>

阅读全文 »

函数处理 函数 function_exists 如果给定的函数已经被定义就返回 true

发表日期:2021-07-01 08:57:16 | 来源: | 分类:函数处理 函数

      示例1
1<?php
2if (function_exists('imap_open')) {
3    echo "IMAP functions are available.<br />\n";
4}
5 else {
6    echo "IMAP functions are not available.<br />\n";
7}
8?>

阅读全文 »

函数处理 函数 get_defined_functions 返回所有已定义函数的数组

发表日期:2021-07-01 08:57:16 | 来源: | 分类:函数处理 函数

      示例1
1<?php
2function myrow($id$data){
3    return "<tr><th>$id</th><td>$data</td></tr>\n";
4}
5$arr = get_defined_functions();
6print_r($arr);
7?>

阅读全文 »

函数处理 函数 register_shutdown_function 注册一个会在php中止时执行的函数

发表日期:2021-07-01 08:57:16 | 来源: | 分类:函数处理 函数

      示例1
1<?php
2function shutdown(){
3    // This is our shutdown function, in     // here we can do any last operations    // before the script is complete.    echo 'Script executed with success', PHP_EOL;
4}
5register_shutdown_function('shutdown');
6?>

阅读全文 »

函数处理 函数 register_tick_function Register a function for execution on each tick

发表日期:2021-07-01 08:57:16 | 来源: | 分类:函数处理 函数

      示例1
1<?php
2declare(ticks=1);
3// using a function as the callbackregister_tick_function('my_function', true);
4// using an object->method$object = new my_class();
5register_tick_function(array(&$object'my_method'), true);
6?>

阅读全文 »

函数处理 函数 unregister_tick_function De-register a function for execution on each tick

发表日期:2021-07-01 08:57:16 | 来源: | 分类:函数处理 函数

unregister_tick_function

(PHP 4 >= 4.0.3, PHP 5, PHP 7, PHP 8)

unregister_tick_functionDe-register a function for execution on each tick

说明

unregister_tick_function(string $function_name): void

De-registers the function named by function_name so it is no longer executed when a tick is called.

参数

function_name

The function name, as a string.

返回值

没有返回值。

参见

阅读全文 »

全部博文(1589)
集速网 copyRight © 2015-2022 宁ICP备15000399号-1 宁公网安备 64010402001209号
与其临渊羡鱼,不如退而结网
欢迎转载、分享、引用、推荐、收藏。