【问题标题】:Static Alternative in PHP for <b>Parse error</b>: syntax error, unexpected T_STATIC in <b>PHP 中 <b>解析错误</b> 的静态替代方案:语法错误,<b> 中的意外 T_STATIC
【发布时间】:2011-09-28 07:46:15
【问题描述】:

我正在使用 Xamppp 在 localhost 上测试我的 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 关键字。你有什么建议让我改变这种方法:

实用程序

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;
    }
}

对于一个独特的门面实例

单例

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);
    }

【问题讨论】:

  • 我刚刚更新了这个问题。版本 5.2.17

标签: php singleton static-methods php4


【解决方案1】:

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

【讨论】:

  • 是的。我知道,但我想要替代方案的建议。
猜你喜欢
  • 2013-12-28
  • 1970-01-01
  • 2011-06-07
  • 2011-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-28
  • 1970-01-01
相关资源
最近更新 更多