Php dynamic class construction

Posted by pistacchio on Stack Overflow See other posts from Stack Overflow or by pistacchio
Published on 2010-05-13T12:29:02Z Indexed on 2010/05/13 12:34 UTC
Read the original article Hit count: 179

Filed under:

Hi to all,

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

© Stack Overflow or respective owner

Related posts about php