Friday 1 December 2017

php - Parse error: syntax error, unexpected T_STATIC






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!



Answer




The parse error
here:



echo "Watching
".static::$favSport;


is
because rel="nofollow noreferrer">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)...



No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...