代码之家  ›  专栏  ›  技术社区  ›  Okeydoke

Zend_Registry-是否需要getInstance()?

  •  2
  • Okeydoke  · 技术社区  · 16 年前

    例如

        Zend_Registry::getInstance()->get('db');

        Zend_Registry::get('db');

    它们似乎都在工作,后者则不那么冗长。 我模模糊糊地理解Zendèu注册表是一个单例,我想这意味着它只能有一个实例?那么为什么需要getInstance()?

    4 回复  |  直到 15 年前
        1
  •  6
  •   Bill Karwin    16 年前

    你可以用静电 get() set() ,它会影响作为类的静态成员存储的注册表的默认实例。这是大多数情况下的预期用途。

    Zendèu Registry的所有其他方法基本上都支持非典型情况,例如,如果您想为注册表实现自己的类,但仍然将其作为默认的单例。或者,如果您想获得实际的注册表对象,例如,这样您就可以序列化整个注册表对象及其所有内容。

        2
  •  2
  •   hsz    16 年前

    你不必使用 getInstance() 如果你看看里面 Zend_Registry 您将看到:

      public static function get($index)
        {
            $instance = self::getInstance();
    
            if (!$instance->offsetExists($index)) {
                require_once 'Zend/Exception.php';
                throw new Zend_Exception("No entry is registered for key '$index'");
            }
    
            return $instance->offsetGet($index);
        }
    

    它在静态方法中为您获取实例。

        3
  •  0
  •   awgy    16 年前

    需要 getInstance() . 我相信,API的其他领域也有。这也是创建单例以公开 getInstance()

        4
  •  0
  •   cEz    16 年前

    您可能希望使用Zend\u Registry::getInstance()的另一个原因是,您将注册表存储在缓存中。