Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Php dynamic class construction

Tags:

php

I'm trying to avoid the use of eval. I can dynamically instantiate a class like this:

class myclass {}

$my_class_name = 'myclass';
$obj = new $myclass();

If the constructor is like follows:

class myclass {
    public function __construct( $argument1, $argument2 ) {}
}

and i have the values of the arguments in an array, how can i dynamically instantiate the class and pass it dynamic arguments? Mind that I have no way to modify the class, so I have to work on the way of using it.

Thanks

like image 452
pistacchio Avatar asked May 13 '10 12:05

pistacchio


1 Answers

ReflectionClass::newInstanceArgs seems to be just the thing you're looking for.

like image 185
Matti Virkkunen Avatar answered Sep 22 '22 02:09

Matti Virkkunen