I’m to make a (sort of) factory class that accepts a variable number of arguments and passes them on to the class that it will be invoking
<?php
class A {
private $a;
private $b;
private $c;
public function __construct($a=1, $b=2, $c=3){
$this->a = $a;
$this->b = $b;
$this->c = $c;
}
}
class B {
private $foo;
public function __construct(){
$args = func_get_args();
$this->foo = call_user_func_array(array('A', '__construct'), $args);
}
public function getObject(){
return $this->foo;
}
}
$b = new B(10, 20, 30);
var_dump($b->getObject()); // should return equivalent of new A(10,20,30);
I’m getting this error
PHP Warning: call_user_func_array() expects parameter 1 to be a valid callback, non-static method A::__construct() cannot be called statically
Found this answer reading about ReflectionClass. This seems to work best