I know you can call static methods using variable as class name like so:
$className = "Foo";
$className::Bar(); //works
But when i'm trying to use static property as variable like this:
self::$className = "Foo";
self::$className::Bar(); //doesn't
it gives me the following parse error on line where i'm trying to call the method:
Parse error: syntax error, unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM)
So how can i call that method using static property and is that even possible with syntax somewhat similar to what i described(w/o call_user_func
and creating local variable that stores self::$className
)?
You could do that:
$tmp = self::$className;
$tmp::Bar();
Edit
Based on your comments it seems your problem is more about OOP design than it is about syntax. Furthermore, you keep adding new restrictions every time a solution is given, which makes it difficult to provide a relevant answer.
Anyway, I'll try to summarize your options. The syntax you want does not exist (at the moment, anyway), so you have to work around it one way or another. Yes, this is annoying, and yes this means that you will have to make concessions. But that's how it is.
Here are your options so far:
call_user_func
or forward_static_call
or similar.static function call($method) { $tmp = self::$classname; return $tmp::$method(); }
and then use self::call('bar');
)eval(self::$classname.'::bar();');
and hope it won't come bite you in the butt.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With