12
class Employee 
{
    public static $favSport = "Football";

    public static function watchTV()
    {
        echo "Watching ".static::$favSport;
    }
}

class Executive extends Employee 
{
    public static $favSport = "Polo";
}

echo Executive::watchTV();

Parse error: syntax error, unexpected T_STATIC on line 7

Why do I get parse error & and how to fix it? Thanks!

1
  • What's the return of phpversion() ?
    – powtac
    Commented Jan 12, 2011 at 12:12

2 Answers 2

26

The parse error here:

echo "Watching ".static::$favSport;

is because late static bindings were introduced in PHP v5.3. Your php version (<5.3) doesn't recognize static::$favSport.

There isn't any way I can think of to fix it for PHP older than 5.3, other than with object inheritance (which isn't really a fix per se since it doesn't have anything to do with static)...

2
  • @Pekka: PHP 4 would choke on the public keyword :)
    – BoltClock
    Commented Jan 12, 2011 at 12:06
  • How about replacing static with the classname? Commented Dec 16, 2015 at 9:04
5

I had the same problem, but i used self in the place of static for my php version that's 5.2.1 well older than 5.3 http://php.net/manual/en/language.oop5.late-static-bindings.php

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