Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

This question already has an answer here:

Since PHP version 5.3 we can call static method in a variable class like this:

class A 
{
    public static function foo()
    {
        echo 'bar';
    }
}

$myVariableA = A::class;

$myVariableA::foo(); //bar

Today I've faced something tricky trying to do this in a class property, like that:

class A 
{
    public static function foo()
    {
        echo 'bar';
    }
}

class B 
{
    protected $myVariableA;

    public function __construct()
    {
        $this->myVariableA = A::class;
    }

    public function doSomething()
    {
        return $this->myVariableA::foo(); //parse error
    }
}

$b = new B;
$b->doSomething();

This returns a parse error:

PHP Parse error: syntax error, unexpected '::'

If we change the doSomething() method to associate the class variable to a local variable, it works fine:

...
    public function doSomething()
    {
        $myVariableA = $this->myVariableA;
        return $myVariableA::foo(); //bar (no parse error)
    }
...

The question is: why do the second format (with local variable) does work, but the first (with method property) does not?

Note that I'm not trying to solve the issue here (as my code is already working), but I want to understand exactly why I can use '::' in a normal variable, but not in a class property.

I'm using PHP 5.6.10.

share|improve this question

marked as duplicate by John Conde php Jul 13 '15 at 17:28

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
just a parser "feature", much like echo "$foo[1][2]" outputs Array[2] instead of whatever's stored at the [2] index. – Marc B Jul 13 '15 at 17:18
    
@john-conde I don't think its duplicate. In the referred question, the guy is trying to access a method of a instanced object with '::' when he should use '->'. I'm trying to call a static method of a class that was not instanced. The code works with a local variable, but does not with a class property and I want to understand why. – Rafael Beckel Jul 13 '15 at 18:54
    
@MarcB I would like to understand why the second case works (with local variable) and the first one (with the class property) does not. If we can use '::' in a local variable to call a static method, why we cannot use it in a class property? – Rafael Beckel Jul 13 '15 at 19:09

Based on this, the error message is something to with the double semicolon ( ::).

On your doSomething(), try using myVariableA->foo();

share|improve this answer
    
The question is not about what T_PAAMAYIM_NEKUDOTAYIM is. I know it means :: and it's also mapped as T_DOUBLE_COLON (I'll edit the question to clarify, thanks). – Rafael Beckel Jul 13 '15 at 18:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.