PHP 中 解析错误的静态替代方案:语法错误, 中出现意外的 T_STATIC

发布于 2024-12-07 02:19:41 字数 1799 浏览 0 评论 0原文

我正在使用 Xamppp 在本地主机上测试我的 PHP 应用程序,一切都很好,但是当我将其导出到真实服务器时,它不再工作了。我发现这是因为我的服务器不支持后期静态绑定。 我的服务器版本为5.2.17

我收到这个错误。

<b>Parse error</b>:  syntax error, unexpected T_STATIC in <b>/home/storage/f/9d/09/meuplacar/public_html/filme/work/class_lib.php</b> on line <b>555</b><br />

我只是在我构建的 Util 类中使用 static 关键字。你对我改变这种方法有什么建议:

Util

class Util
{
    private static $initialized = false;
        private static function initialize()
        {
            if (self::$initialized)
                    return;
            self::$initialized = true;
        }

    public static function getHoursAndMinutesFromTime($time) {
        self::initialize();
        $pieces = explode (":", $time);
        $output = "";
        $output = $pieces[0] . ":" . $pieces[1];
        return $output;
    }
}

对于一个独特的外观实例

Singleton

abstract class Singleton {

    protected static $_instance = NULL;

    /**
     * Prevent direct object creation
     */
    final private function  __construct() {
    }

    /**
     * Prevent object cloning
     */
    final private function  __clone() {
    }

    /**
     * Returns new or existing Singleton instance
     * @return Singleton
     */
    final public static function getInstance(){
        if(null !== static::$_instance){
            return static::$_instance;
        }
        static::$_instance = new static();
        return static::$_instance;
    }

}

class Facade extends Singleton  {

    public function retrieveAllWorkdays()
    {
        $array = DB::selectAllWorkdays();
        return Util::constructWorkdaysArray($array);
    }

I was testing my PHP application on localhost with Xamppp and everything was just fine, but when I exported it to a real server, it does not work anymore. I found out that it is because my server does not support late static binding.
My server has version 5.2.17.

I get this error.

<b>Parse error</b>:  syntax error, unexpected T_STATIC in <b>/home/storage/f/9d/09/meuplacar/public_html/filme/work/class_lib.php</b> on line <b>555</b><br />

I just use the static keyword in a Util class I built. What would you suggest for me to change this kind of methods:

Util

class Util
{
    private static $initialized = false;
        private static function initialize()
        {
            if (self::$initialized)
                    return;
            self::$initialized = true;
        }

    public static function getHoursAndMinutesFromTime($time) {
        self::initialize();
        $pieces = explode (":", $time);
        $output = "";
        $output = $pieces[0] . ":" . $pieces[1];
        return $output;
    }
}

And for a Unique Facade Instance

Singleton

abstract class Singleton {

    protected static $_instance = NULL;

    /**
     * Prevent direct object creation
     */
    final private function  __construct() {
    }

    /**
     * Prevent object cloning
     */
    final private function  __clone() {
    }

    /**
     * Returns new or existing Singleton instance
     * @return Singleton
     */
    final public static function getInstance(){
        if(null !== static::$_instance){
            return static::$_instance;
        }
        static::$_instance = new static();
        return static::$_instance;
    }

}

class Facade extends Singleton  {

    public function retrieveAllWorkdays()
    {
        $array = DB::selectAllWorkdays();
        return Util::constructWorkdaysArray($array);
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抱猫软卧 2024-12-14 02:19:41

后期静态绑定仅在 php 5.3.0 及更高版本中可用: http ://php.net/manual/en/language.oop5.late-static-bindings.php

Late static bindings only are available in php 5.3.0 and later: http://php.net/manual/en/language.oop5.late-static-bindings.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文