4

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)?

7
  • Have you tried using call_user_func? Commented Aug 21, 2015 at 9:49
  • @Svengali if possible i wan't to call that method like i described Commented Aug 21, 2015 at 9:50
  • This syntax is wrong. The word self refers to the class name of the current class scope. So basically, what you are trying to say is "call a method (Bar()) from class ($className) of the current class self". It doesn't make much sense, does it? Commented Aug 21, 2015 at 10:03
  • @Avalanche well, yes. I don't see what's wrong here, current class self has property $className and i'm trying to call a method from class which name is equal to $className value. Commented Aug 21, 2015 at 10:09
  • 1
    You have two quick options: either use a local variable as suggested in @rlanvin's answer or use PHP 7. The more elaborate (and probably the correct) option is to extract the static methods and properties into a separate class and use inheritance and polymorphism to implement the functionality you need. You'll discover that your problem will vanish together with the line of code that generated it. Commented Aug 21, 2015 at 10:32

1 Answer 1

4

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:

  • Use call_user_func or forward_static_call or similar.
  • Use a temporary local variable. Possibly wrap that into a method if it's really bothering you (e.g. static function call($method) { $tmp = self::$classname; return $tmp::$method(); } and then use self::call('bar');)
  • Refactor your object design using instances instead of static methods so that you don't need to do that anymore.
  • Use some other terribly ugly and dangerous hack (e.g. eval(self::$classname.'::bar();'); and hope it won't come bite you in the butt.
Sign up to request clarification or add additional context in comments.

8 Comments

that's pretty obvious and not what i want
how is it not what you want?
i need to use self class property and not the local variable
Yeah but what you want is an invalid syntax, it's not possible. So you have to work around it with an extra variable (or use call_user_func, but you don't want that either so...)
i understand that what i wrote is an invalid syntax, that's why i asked the question in the first place, i need something similar and was wondering if there's a way to do this without call_user_func and creating local variable
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.