0

An amazing thing:

Class Myclass{

 protected $_value = 'content';

 public function action(){

  $this->_value::mymethod();
 }

}

=> I've got an error:

syntax error, unexpected '::'

If I modify like this, it works:

$myvalue = $this->_value;
$myvalue::mymethod();

Do you know why??

Langusten Gustel
  • 10,917
  • 9
  • 46
  • 59
  • 2
    '::' is for calling static methods so you are calling a static method of a string...which is wrong... – ka_lin Jun 26 '14 at 09:55
  • _value, I think here underscore is creating problem – DWX Jun 26 '14 at 09:55
  • @KA_lin I think `'content'` is just a placeholder for the actual content. Otherwise his code `$myvalue::mymethod()` wouldn't work either, which it does according to the question. – padarom Jun 26 '14 at 09:56
  • @user3778717: Please provide the full code – ka_lin Jun 26 '14 at 09:57
  • 1
    @DWX why would an underscore be a problem? – Damien Pirsy Jun 26 '14 at 09:58
  • 1
    Guys, this *is* the full code. The "modified" version indeed works, as it's supposed to. This actually is a valid question! The end result is to statically call `mymethod` on the `content` class. – deceze Jun 26 '14 at 09:58
  • Why are you doing this, what do you want to achieve by this? – ka_lin Jun 26 '14 at 09:59
  • Fatal error: Class '_value' not found, works great – ka_lin Jun 26 '14 at 10:02
  • 1
    possible duplicate of [Calling static method from object array variable](http://stackoverflow.com/questions/11519272/calling-static-method-from-object-array-variable) – billyonecan Jun 26 '14 at 10:03

1 Answers1

3

PHP cannot confidently determine what you are trying to accomplish with

$this->_value::mymethod();

It can be read as either

{$this->_value}::mymethod() (what you expect it to be)

or

$this->{_value::mymethod()}.

So instead of guessing and might be wrong, it generates an error. Just use the way around it that you already discovered.

Blizz
  • 8,082
  • 2
  • 33
  • 53
  • Well, I've tried as suggested : {$this->_value}::mymethod(); I suppose you force priority to accomplish it. But I have another errot message: syntax error, unexpected '}' – user3778717 Jun 26 '14 at 12:09
  • Hehe, the way you did it was the only way that works (at this point). I just used the brackets to indicate the priority, sorry about the confusion. – Blizz Jun 26 '14 at 12:11