0

I'm having a very strange syntax error inside of a class when trying to access a static method of a class variable.

class VendorImport { //$factory is an instance of another class with a static method get() protected $factory; public function getInstance() { //method 1 works $factory = $this->factory; return $factory::get(); //method 2 throws a syntax error return $this->factory::get(); } } 

What is the proper syntax for method 2?

6
  • What error do you get? Commented Apr 4, 2014 at 21:42
  • vague guess: parser precedence problem, where your error version is seen as $this->{factory::get()}. What is the EXACT error message you're getting? Commented Apr 4, 2014 at 21:47
  • please check: stackoverflow.com/questions/13638014/… Commented Apr 4, 2014 at 21:53
  • If I try it, I get "syntax error, unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM)", which is very clear but doesn't explain the reason. Commented Apr 4, 2014 at 21:53
  • sorry, the actual error is FatalErrorException: Parse: syntax error, unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM) Commented Apr 5, 2014 at 2:09

1 Answer 1

2

Just use regular syntax for calling non-static methods - it's applicable for static ones too:

// instead of `return $this->factory::get();` return $this->factory->get(); 

Demo. There's a drawback, though: now it's not obvious a static method gets called here. But then again, one cannot define two methods - static and non-static - under the same name in the same class.

Sign up to request clarification or add additional context in comments.

1 Comment

This indeed does work but I hate that it doesn't make it obvious that I am calling a static method. The question remains, why does $this->factory::get() caused a syntax error. I am using PHP 5.4.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.