【问题标题】:PHP 5.3 vs PHP 5.2 - need reflection?PHP 5.3 与 PHP 5.2 - 需要反思吗?
【发布时间】:2011-12-01 04:23:35
【问题描述】:

我认为我需要使用反射来使这个 5.3 代码在 5.2 上工作但有问题。

我们在只有 PHP 5.2 的服务器上,目前每个托管公司都无法升级,但我们需要的类在 5.2 上存在问题,因为该类使用 5.3 语法。

这是我需要帮助的代码:

static public function instance($class) {
    if (!isset($class::$instance)) {
        $class::$instance = new $class();
        $class::$instance->initialize();

        MobileHelper::registerDevice($class::$instance);
    }

    return $class::$instance;
}

我看过很多关于使用反射的问题的答案,但它们都是基本示例,我不太了解将它们转换为这里的解决方案,但我已经尝试过了。这里有没有专家可以提供帮助?

【问题讨论】:

    标签: php php-5.3 php-5.2


    【解决方案1】:

    这里,同样使用反射。

    static public function instance($class) {
        $ref = new ReflectionClass($class);
        if (!$ref->getStaticPropertyValue('instance')){
            $ref->setStaticPropertyValue('instance', new $class());
            $obj = $ref->getStaticPropertyValue('instance');
            $obj->initialize();
            MobileHelper::registerDevice($obj);
        }
        return $ref->getStaticPropertyValue('instance');
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-17
      • 2012-07-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多