Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse error: syntax error, unexpected T_STATIC [closed]

Tags:

php

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!

like image 312
easyrider Avatar asked Jan 12 '11 12:01

easyrider


People also ask

How do I fix parse error syntax error unexpected?

The best way to solve it is to remove the recently added plugins by disabling them. The WordPress site is also likely to generate an error after a code edit. A mistake as simple as a missing comma is enough to disrupt the function of a website.

What is syntax error unexpected?

Syntax Error – This error is caused by an error in the PHP structure when a character is missing or added that shouldn't be there. Unexpected – This means the code is missing a character and PHP reaches the end of the file without finding what it's looking for.

How do I fix parse error in WordPress?

Ending thoughts on WordPress parse error Often it's due to a plugin or a theme. By deactivating the plugin/theme, you should be able to resolve the issue. The error message could also indicate a mistake in the code. Most of the time, it's necessary to track the faulty line and modify it.

What is parse error in PHP?

Parse Error (Syntax) Parse errors are caused by misused or missing symbols in a syntax. The compiler catches the error and terminates the script. Parse errors are caused by: Unclosed brackets or quotes. Missing or extra semicolons or parentheses.


2 Answers

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)...

like image 63
BoltClock Avatar answered Oct 05 '22 22:10

BoltClock


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

like image 44
dennis muthuri Avatar answered Oct 05 '22 22:10

dennis muthuri