0

I made an example to better explain the situation. If I run this

class A
{
    public function __construct()
    {
        return new B(func_get_args());
    }
}

class B
{
    public function __construct()
    {
        var_dump(func_get_args());
    }
}

$obj = new A('a', 'b', 'c');

I get

array(1) {
  [0]=>
  array(3) {
    [0]=>
    string(1) "a"
    [1]=>
    string(1) "b"
    [2]=>
    string(1) "c"
  }
}

But what if the constructor prototype of B is public function __construct($var1, $va2, $var3)? How could I pass the parameters to it if I don't know the exact numbers of parameters of the B constructor?

Phate01
  • 2,499
  • 2
  • 30
  • 55
  • possible duplicate of [How to "invoke" a class instance in PHP?](http://stackoverflow.com/questions/1542717/how-to-invoke-a-class-instance-in-php) – tmt Apr 23 '15 at 09:59
  • You've edited this question to rename class `A` to `Instantiator`. If you now try to create an instance of `A` it won't be found. – halfer Apr 26 '15 at 08:28
  • you should check out ReflectionClass::getConstructor and ReflectionClass::newInstanceArgs. This will enable you to find out the number of args and instantiate the object – Orangepill Apr 26 '15 at 08:48

1 Answers1

0

This may not be the one which you are looking for, But with the best of my knowledge I think there is no other solution.

Class A {
        public function __construct()
        {
            return new B(func_get_args());
        }
    }

    class B
    {
        public function __construct($paramArr = array())
        {
            var_dump($paramArr);
        }
    }

    $obj = new A('a', 'b', 'c');
Sanjay Kumar N S
  • 4,653
  • 4
  • 23
  • 38
  • I updated my question: I meant what if I don't know how many parameters does the B constructor have? – Phate01 Apr 23 '15 at 10:02
  • I don't think this will work - constructors cannot be asked to explicitly return something, since they are designed to return an instance of the class requested. – halfer Apr 24 '15 at 21:23