tags:

views:

59

answers:

2

So normal class methods and object creation goes like this...

$obj = new Class();
$obj2 = Class::someMethod();

Can I instantiate "class" dynamically? How? I want to do something like...

$class = "Class";
$obj = new $class?;
$obj2 = $class?::someMethod();
+4  A: 
$class = "Class";
$obj = new $class; // works
$obj2 = $class::someMethod(); // works as of PHP 5.3

// if you don't have PHP 5.3 and want to use the ladder:
$obj2 = call_user_func(array($class, 'someMethod'));
nikic
+1  A: 

That should work without the ? in $obj = new $class?.

Jacob Relkin