1

I have a property that stores a class name as a string. I then want to use this to call a static method of said class. As far as I know, this is possible since PHP 5.3. I am running 5.6.x on a vagrant box.

I want to do this:

$item = $this->className::getItem($id);

But I get the following error:

Parse error: syntax error, unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM)...

The following works fine:

$c = $this->className;
$item = $c::getItem($id);

Any idea why? Is this not the same thing?

Gurnzbot
  • 3,742
  • 7
  • 36
  • 55
  • 3
    `class` is a reserved (key)word in OOP, far as I remember. http://php.net/manual/en/reserved.keywords.php – Funk Forty Niner Dec 01 '16 at 20:08
  • Because line `$this->class::getItem($id);` is not clear. What is it `{$this->class}::getItem($id);` or `$this->{class::getItem($id)};` Who can tell? – u_mulder Dec 01 '16 at 20:09
  • [What does this mean? “Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM”](http://stackoverflow.com/questions/1966010/what-does-this-mean-parse-error-syntax-error-unexpected-t-paamayim-nekudotay) – Funk Forty Niner Dec 01 '16 at 20:14
  • Ya I realized `class` was reserved and updated my code to use a non-reserved word. I still had the issue so I left the post alone. I have edited it now to clear up that I am no longer using the reserved word. – Gurnzbot Dec 01 '16 at 20:21
  • well [that stealthy edit](http://stackoverflow.com/revisions/40919487/2) blows [my comment](http://stackoverflow.com/questions/40919487/php-variable-class-static-method-call#comment69051201_40919487) right out of the water, doesn't it? – Funk Forty Niner Dec 01 '16 at 20:21
  • @u_mulder: Thing is, it shouldn't be at all unclear. In C++, for example, `::` has a higher precedence than `->`, so the second option would be preferred. In PHP, on the other hand, neither one even seems to be an operator. But even if you tried to use parentheses or braces to clear up the precedence issue, i'm pretty sure PHP still couldn't parse it. (At least not as of 5.6.) – cHao Dec 01 '16 at 20:23
  • Possible duplicate of [What does this mean? “Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM”](http://stackoverflow.com/questions/1966010/what-does-this-mean-parse-error-syntax-error-unexpected-t-paamayim-nekudotay) – Funk Forty Niner Dec 01 '16 at 20:23
  • @Fred-ii- : I think this being a duplicate of that question is a stretch. Maybe I'm wrong. Thank you for the link in any case. Good read :) – Gurnzbot Dec 01 '16 at 20:29

2 Answers2

2

The problem is that you are access are property from a class in the first useage, but then in the second try you are parsing the value of the class property (into $c), what is a classname as string, and this can used for static calls to static class functions. The first try, trys to access the static method on an string (the class property).

class a {
     static function b(){echo'works';}
}
$a='a';
$a::b();

But the real issue of the error is, that this ->FooBar:: is an syntax error in PHP.

JOUM
  • 239
  • 1
  • 3
0

JOUM is completely right! Based on his answer I wrote a class like a fabric.

Interface GetItem
{
    public static function getItem($id);
}

Abstract Class Item
{
    private $id;

    function __construct($id)
    {
        $this->id = $id;
    }
}

Class ItemA extends Item implements GetItem
{

    public static function getItem($id)
    {
        $item = new ItemA($id);
        return $item;
    }
}

Class ItemB extends Item implements GetItem
{

    public static function getItem($id)
    {
        $item = new ItemB($id);
        return $item;
    }
}    


Class Fabric
{
    function fabricItem($classname,$id)
    {
        $item = $classname::getItem($id);

        return $item;
    }
}


$fabric = new Fabric();

$a = $fabric->fabricItem("ItemA",3);
$b = $fabric->fabricItem("ItemB",4);    


var_dump($fabric);
var_dump($a);
var_dump($b);