所以我尝试写了一个“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
将该数组内爆,将两个参数放入该类中。换句话说,我以为我在传递两个论点??