动态构建要传递给call_user_func的参数列表(包括作为单个参数的arrays())


Dynamically build a list of parameters to pass to call_user_func (including arrays() as single params)?

如何在不知道需要传递给call_user_func:的参数数量的情况下动态构建参数数组

举个简单的例子,假设我试图调用的函数如下:

class foo {
   // Param 1 must be boolean,
   // Param 2 must be array(),
   // Param 3 is string
   function bar($param1, $param2, $param3) {
      if($param1 == true) {
         foeach($param2 as $pvalue) {
            echo $param3 . ' - ' . $pvalue;
         }
      }
   }
}

我用我写的类(类中需要帮助的内联注释)来调用它

$event = new event('foo');
// Params to Pass
$array  = Array(1,2,3);
$bool   = true;
$string = 'Number: ';
$event::AddParam($bool);
$event::AddParam($array);
$event::AddParam($string);
$event::RaiseEvent('bar');

使用我编写的以下类(基本上是模拟事件——所以我相信这是我最不需要知道的事情,可以100%实现),

<?php
/**
 *     Copyright (C) {2014}  {MicroVB INC}
 * 
 *         This program is free software: you can redistribute it and/or modify
 *         it under the terms of the GNU General Public License as published by
 *         the Free Software Foundation, either version 3 of the License, or
 *         (at your option) any later version.
 * 
 *     This program is distributed in the hope that it will be useful,
 *         but WITHOUT ANY WARRANTY; without even the implied warranty of
 *         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *         GNU General Public License for more details.
 * 
 *     You should have received a copy of the GNU General Public License
 *         along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * unitrack-invoices
 * event.php
 * 4/13/14 1:11 AM
 *
 */
class event {
    private static $class;
    private static $obj;
    private static $params;
    public static function init($class=false) {
        self::$class = $class;
        if($class) {
            if(class_exists($class)) {
                self:$obj = new $class();
            }
        }
        self::$params = Array();
    }
    public static function AddParam($value) {
        self::$params[] = $value;
    }
    public static function RaiseEvent($function) {
        if(self::$class) {
            if(method_exists(self::$obj, $function)) {
                foreach(self::$params as $param) {
                    // Build each $param into something passable to call_user_func_array
                    // Please note that each item in this array could be an array as well
                }
                call_user_func_array(Array(self::$obj, $function), $paramlist );
                self::$params = Array();
            } else {
                // Method does not exist - throw error;   
            }
        } else {
            if(function_exists($function)) {
                foreach(self::$params as $param) {
                    // Build each $param into something passable to call_user_func_array
                    // Please note that each item in this array could be an array as well
                }
                call_user_func_array(Array(self::$obj, $function), $paramlist );
                self::$params = Array();
            } else {
                // Function does not exist - throw error;   
            }
        }
    }
} 

具体来说,我遇到的问题是这里>>

        foreach(self::$params as $param) {
            // Build each $param into something passable to call_user_func_array
            // Please note that each item in this array could be an array as well
        }
        call_user_func_array(Array(self::$obj, $function), $paramlist );

我怎样才能使值$paramlist——或者任何在这里可行的值。记住,如果我传递一个数组,它只会导致它是foo::bar()$param1。。。所以我需要能够动态地将其分解为$param1$param2$param3。。。等在不知道foo::bar()有多少个参数的情况下(也就是说,我不能预先静态分配值)

找到了解决方案——这是任何有兴趣用PHP创建事件系统的人的工作类。非常欢迎人们更新这一点并添加错误处理等。我在这里仍然没有为__construct参数支持设置任何内容,但是如果被调用的类是专门用于处理事件的,那么很可能不需要__construct参数。

<?php
/**
 *     Copyright (C) {2014}  {MicroVB INC}
 * 
 *         This program is free software: you can redistribute it and/or modify
 *         it under the terms of the GNU General Public License as published by
 *         the Free Software Foundation, either version 3 of the License, or
 *         (at your option) any later version.
 * 
 *     This program is distributed in the hope that it will be useful,
 *         but WITHOUT ANY WARRANTY; without even the implied warranty of
 *         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *         GNU General Public License for more details.
 * 
 *     You should have received a copy of the GNU General Public License
 *         along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * unitrack-invoices
 * event.php
 * 4/13/14 1:11 AM
 *
 */
class event {
    private static $class;
    private static $obj;
    private static $params;
    public static function init($class=false) {
        self::$class = $class;
        if($class) {
            if(class_exists($class)) {
                self:$obj = new $class();
            }
        }
        self::$params = Array();
    }
    public static function AddParam($value) {
        self::$params[] = $value;
    }
    public static function RaiseEvent($function) {
        if(self::$class) {
            if(method_exists(self::$obj, $function)) {
                if(count(self::$params) > 0) {
                    call_user_func_array(Array(self::$obj, $function), self::$params );
                } else {
                    call_user_func(Array(self::$obj, $function));
                }
                self::$params = Array();
            } else {
                // Method does not exist - throw error;
            }
        } else {
            if(function_exists($function)) {
                if(count(self::$params) > 0) {
                    call_user_func_array($function, self::$params );
                } else {
                    call_user_func($function);
                }
                self::$params = Array();
            } else {
                // Function does not exist - throw error;
            }
        }
    }
}