func_get_args call_user_func_array

<?php
//call_user_func_array.php
   function test($arg1,$arg2)
   {
    $t_args = func_get_args();
    $t_result = call_user_func_array( ‘gpc_get‘, $t_args );
    var_dump($t_result);
   }

    function gpc_get($args)
    {
        echo ‘in gpc_get function:<br>‘;
        var_dump($args);
        echo ‘<hr>‘;
        if(!is_array($args))
        {
            return ‘gpc_get~‘ . $args ;
        }
        else
        {
            $ret=array();
            $len=count($args);
            for($i=0;$i<$len;$i++)
            {
                $ret[]= ‘gpc_get~‘ . $args[$i] ;
            }
            return $ret;
        }
    }

    test(‘hello‘,‘world‘);
    test(array(‘aaaaa‘,‘bbbbb‘,‘ccccc‘,‘ddddd‘),‘world‘);

/*
in gpc_get function:
string ‘hello‘ (length=5)

string ‘gpc_get~hello‘ (length=13)

in gpc_get function:
array (size=4)
  0 => string ‘aaaaa‘ (length=5)
  1 => string ‘bbbbb‘ (length=5)
  2 => string ‘ccccc‘ (length=5)
  3 => string ‘ddddd‘ (length=5)

array (size=4)
  0 => string ‘gpc_get~aaaaa‘ (length=13)
  1 => string ‘gpc_get~bbbbb‘ (length=13)
  2 => string ‘gpc_get~ccccc‘ (length=13)
  3 => string ‘gpc_get~ddddd‘ (length=13)
*/
?>
时间: 2024-08-29 03:20:10

func_get_args call_user_func_array的相关文章

PHP实现事件机制实例分析

PHP实现事件机制实例分析 内置了事件机制的语言不多,php也没有提供这样的功能.事件(Event)说简单了就是一个Observer模式,实现起来很容易.但是有所不同的是,事件的监听者谁都可以加,但是只能由直接包含它的对象触发.这就有一点点难度了.php有一个debug_backtrace函数,可以得到当前的调用栈,由此可以找到判断调用事件触发函数的对象是不是直接包含它的对象的办法. <?php /** * 事件 * @edit http://www.lai18.com * @author xi

上传一个php图片处理类

<?phpclass Image { private $file; private $image; private $width; private $height; private $bits; private $mime; /** * 图片路径 * Image constructor. * @param $file */ public function __construct($file) { if (file_exists($file)) { $this->file = $file; $i

简单理解call_user_func和call_user_func_array两个函数

call_user_func():调用一个回调函数处理字符串, 可以用匿名函数,可以用有名函数,可以传递类的方法, 用有名函数时,只需传函数的名称 用类的方法时,要传类的名称和方法名 传递的第一个参数必须为函数名,或者匿名函数,或者方法 其他参数,可传一个参数,或者多个参数,这些参数会自动传递到回调函数中 而回调函数,可以通过传参,获取这些参数 返回回调函数处理后的结果 ①传递函数名,通过回调函数的形参获取call_user_func传参数的情况 <?php //先引用后增加 function

PHP 函数之 call_user_func &amp; call_user_func_array

call_user_func_array (callable $callback, array $param_arr) 参数1: 调用一个回调函数, 参数2: 数组参数是回调函数的参数. call_user_func(callable $callback, $mixed $parameter, $mixed $...) 参数1:调用的回调函数 参数2-n:回调函数的参数. 比较这两者的不同哦. 前者的第二个参数必须是 数组. 情况一:调用普通的函数. <?php function barber(

PHP函数func_get_args(),func_get_arg(),func_num_args()

func_get_arg 说明 mixed func_get_arg(int $arg_num) 从用户自定义函数的参数列表中获取某个参数. 参数 arg_num          参数的偏移量.函数的参数从0开始计数. 返回值 返回指定的参数,错误则返回FALSE. func_get_args 说明 array func_get_args(void)获取函数参数列表的数组. 参数 无 返回值 返回一个数组,其中每个元素都是目前用户自定义的参数列表的相应元素的副本. func_num_args

php学习之道:call_user_func和call_user_func_array的用法

call_user_func ( callback $function [, mixed $parameter [, mixed $... ]] ) 调用第一个参数所提供的用户自定义的函数. 返回值:返回调用函数的结果,或FALSE. example : Php代码   <?php function eat($fruit) //参数可以为多个 { echo "You want to eat $fruit, no problem"; } call_user_func('eat', 

call_user_func_array

(PHP 4 >= 4.0.4, PHP 5) call_user_func_array - 调用回调函数,并把一个数组参数作为回调函数的参数 mixed call_user_func_array ( callable $callback , array $param_arr ) 把第一个参数作为回调函数(callback)调用,把参数数组作(param_arr)为回调函数的的参数传入. callback 被调用的回调函数. param_arr 要被传入回调函数的数组,这个数组得是索引数组. 返

php中call_user_func_array()的使用

1 <?php 2  3 function demo($num, $n) { 4     for ($i = 0; $i < $num; $i++) { 5         //if ($n($i)) 6         if (call_user_func_array($n, array($i))) 7             continue; 8         echo $i.'<br>'; 9     }10 }11 12 class Filter13 {14     f

PHP回调函数--call_user_func_array

全局函数的回调 这里的全局函数的意思,是直接使用function定义的函数,它不包含在任何对象或类之中.请看下面的例子 示例代码 $msg1 , $msg2 ){    echo 'msg1:'.$msg1;    echo "<br />\n";    echo 'msg2:'.$msg2;}$fnName = "fnCallBack";$params = array( 'hello' , 'world' );call_user_func_array(