Matteo Riva
Matteo Riva

Reputation: 25060

Using a class name stored in a property

I have a class name stored in an object property, and I would like to use it to access a static method from that class, but I can't seem to find a working syntax:

$this->className::staticMethod()

doesn't work and

{$this->className}::staticMethod()

doesnt' work either, does PHP have a correct syntax to do this?

Upvotes: 2

Views: 56

Answers (2)

George G
George G

Reputation: 7695

If you have PHP version > 5.2

call_user_func($this->className.'::staticMethod'); 

else

call_user_func(array($this->className, 'staticMethod'));

Also with arguments:

call_user_func_array(array($this->className, 'staticMethod'), array($argument, $anotherArg));

Upvotes: 2

Justinas
Justinas

Reputation: 43479

Try this syntax:

$class = $this->className;
$class::staticMethod();

Upvotes: 2

Related Questions