php 解析错误:语法错误,意外的 T_STATIC

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4668557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 13:54:55  来源:igfitidea点击:

Parse error: syntax error, unexpected T_STATIC

php

提问by easyrider

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

解析错误:语法错误,第 7 行出现意外的 T_STATIC

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

为什么会出现解析错误以及如何解决?谢谢!

回答by BoltClock

The parse error here:

这里的解析错误:

echo "Watching ".static::$favSport;

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

是因为在 PHP v5.3 中引入了后期静态绑定。您的 php 版本 (<5.3) 无法识别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 fixper se since it doesn't have anything to do with static)...

除了对象继承(这本身并不是真正的修复,因为它与 5.3 之前的 PHP 没有任何关系)之外,我想不出任何方法可以为 5.3 之前的 PHP 修复它static...

回答by dennis muthuri

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

我有同样的问题,但我用 self 代替了我的 5.2.1 比 5.3 早得多的 php 版本的静态http://php.net/manual/en/language.oop5.late-static-bindings.php