views:

224

answers:

1

Hey guys, Im having a problem with using a variable as the class name when calling a static function within the class. My code is as follows:

class test {
     static function getInstance() {
         return new test();
     }
}

$className = "test";
$test = $className::getInstance();

Ive got to define the class name to a variable as the name of the class is coming from a database so i never know what class to create an instance of.

note: currently i am getting the following error:

Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM 

Thanks

+4  A: 
$test = call_user_func(array($className, 'getInstance'));

See call_user_func and callbacks.

hobodave
Beat me to the punch. That's the one (and only?) right way.
Pekka
Only way until 5.3 that is, in 5.3+ you can use variable classnames.
StasM