Reputation: 928
So I attempted, and possibly failed, to write a "mixin" class of sorts. It works as expected for the most part until you have multiple parameters for a class to pass in, then the world implodes. My class is as such:
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);
}
}
}
And is extended and used in a "bridge" class as such:
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();
}
}
Whats the issue?
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()
I thought doing something like:
array($options, $this->_components)
Takes the parameters for that class, wraps it in an array and then newInstanceArgs
implodes that array putting both parameters into the class. In other words I thought i was passing two arguments??
Upvotes: 3
Views: 2081
Reputation:
The error message is telling you exactly what's going wrong:
Warning: Missing argument 2 for BlahBlahBlah::__construct()
So the issue is that all your parameters aren't being passed to the constructor when you're instantiating an object here:
$this->_class_objects[] = $object->newInstance($params);
If you consult the relevant documentation for ReflectionClass::newInstance
docs you'll see this:
Creates a new instance of the class. The given arguments are passed to the class constructor.
So no matter how many elements you have in the $params
array you're only passing Argument 1 to the constructor with your current approach. The solution is to use ReflectionClass::newInstanceArgs
docs instead as this will expand the array of parameters and pass them as individual arguments to the constructor:
$this->_class_objects[] = $object->newInstanceArgs($params);
Upvotes: 4