I’m writing a factory class that should be able to return singleton instances of a number of different types, depending on the given parameter. The method would look something like this, but the way I’m referencing the singleton’s static method is obviously wrong:
public function getService($singletonClassName) {
return $singletonClassName::getInstance();
}
What would the correct syntax for such a reference look like in PHP?
You cannot use that kind of syntax with PHP < 5.3 : it’s one of the new features of PHP 5.3
A couple of possibilities, with PHP 5.2, would be to :
call_user_funcIn the first case, it would be as simple as :
And, in the second, you’d use something like :
According to the documentation of
call_user_func, this should work with PHP >= 5.2.3Or you could just use :