Several paamayim nekudotayims in PHP, why not?

In PHP 5.3.6 , I noticed that the following will not work:

class Foo{ public static $class = 'Bar'; } class Bar{ public static function sayHello(){ echo 'Hello World'; } } Foo::$class::sayHello(); 

Issue unexpected T_PAAMAYIM_NEKUDOTAYIM . However, using a temporary variable leads to the expected:

 $class = Foo::$class; $class::sayHello(); // Hello World 

Does anyone know if this is by design or an unintended result of how the scope operator is indicated or something else? Any cleaner workarounds than the last, temporary example of a variable?

+7
source share
1 answer

Unfortunately, there is no way to do this on one line. I thought you could do this with call_user_func (), but don't leave:

 call_user_func(Foo::$class.'::sayHello()'); // Warning: call_user_func() expects parameter 1 to be a valid callback, class 'Bar' does not have a method 'sayHello()' 

Also, why do you want to do something like this in the first place? I am sure there should be a better way to do what you are trying to do - usually there is if you use variable variables or class names.

+2
source

All Articles