2

所以我尝试写了一个“mixin”类,但可能失败了。它在大多数情况下按预期工作,直到您有多个参数供一个类传入,然后世界崩溃。我的课是这样的:

class AisisCore_Loader_Mixins {

    private $_classes;

    private $_class_objects = array();

    private $_methods = array();

    public function __construct(){
        $this->init();
    }

    public function init(){}

    public function setup($class){
        if(!is_array($class)){
            throw new AisisCore_Loader_LoaderException('Object passed in must be of type $class_name=>$params.');
        }

        $this->_classes = $class;
        $this->get_class_objects();
        $this->get_methods();    
    }

    public function get_class_objects(){
        foreach($this->_classes as $class_name=>$params){
            $object = new ReflectionClass($class_name);
            $this->_class_objects[] = $object->newInstance($params);
        }
    }

    public function get_methods(){
        foreach($this->_class_objects as $class_object){
            $this->_methods = get_class_methods($class_object);
        }
    }

    public function call_function($name, $param = null){
        foreach($this->methods as $method){
            $this->isParam($method, $param);
        }
    }

    private function isParam($method, $param){
        if($param != null){
            call_user_func($method, $param);
        }else{
            call_user_func($method);
        }        
    }
}

并且在“桥”类中扩展和使用,如下所示:

class AisisCore_Template_Helpers_Loop_LoopMixins extends AisisCore_Loader_Mixins{

    private $_options;

    private $_wp_query;

    private $_post;  

    private $_components;

    public function __construct($options){
        parent::__construct();
        global $wp_post, $post;

        if(isset($options)){
          $this->_options = $options;
        }

        if(null === $wp_query){
          $this->_wp_query = $wp_query;
        }

        if(null === $post){
          $this->_post = $post;
        }

        $this->_components = new AisisCore_Template_Helpers_Loop_LoopComponents($this->_options);

        $this->setup(array(
            'AisisCore_Template_Helpers_Loop_Helpers_Loops_Single' => array($options, $this->_components),
            'AisisCore_Template_Helpers_Loop_Helpers_Loops_Query' => array($options, $this->_components),
            'AisisCore_Template_Helpers_Loop_Helpers_Loops_Page' => array($options, $this->_components),
        ));
    }

    public function init(){
        parent::init();
    }
}

什么问题?

Warning: Missing argument 2 for AisisCore_Template_Helpers_Loop_Helpers_Loops_Single::__construct() 
Warning: Missing argument 2 for AisisCore_Template_Helpers_Loop_Helpers_Loops_Query::__construct() 
Warning: Missing argument 2 for AisisCore_Template_Helpers_Loop_Helpers_Loops_Page::__construct() 

我想这样做:

array($options, $this->_components)

获取该类的参数,将其包装在一个数组中,然后newInstanceArgs将该数组内爆,将两个参数放入该类中。换句话说,我以为我在传递两个论点??

4

1 回答 1

4

错误消息准确地告诉您出了什么问题:

Warning: Missing argument 2 for BlahBlahBlah::__construct()

所以问题是当你在这里实例化一个对象时,你的所有参数都没有传递给构造函数:

$this->_class_objects[] = $object->newInstance($params);

如果您查阅文档的相关文档,ReflectionClass::newInstance会看到:

创建该类的新实例。给定的参数被传递给类构造函数。

因此,无论$params数组中有多少元素,您都只是使用当前方法将参数 1 传递给构造函数。解决方案是改用ReflectionClass::newInstanceArgsdocs,因为这将扩展参数数组并将它们作为单独的参数传递给构造函数:

$this->_class_objects[] = $object->newInstanceArgs($params);
于 2013-10-08T17:52:29.217 回答