- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
你找到Zend_Registry了吗?有用吗?
它应该用于哪些任务?哪个不是?
变量的全局状态不是一个好习惯。主要对象可以通过 $front->setParam('paramName', $object)
注入(inject)全局状态,那么 Zend_Registry 的目的是什么?
最佳答案
引用 PoEAA on Registry Pattern :
When you want to find an object you usually start with another object that has an association to it, and use the association to navigate to it. Thus, if you want to find all the orders for a customer, you start with the customer object and use a method on it to get the orders. However, in some cases you won't have an appropriate object to start with. You may know the customer's ID number but not have a reference. In this case you need some kind of lookup method - a finder - but the question remains: How do you get to the finder?
我使用注册表(当我使用它时)的主要原因是因为它创建了一个易于访问的应用程序范围。使用注册表,我不必在全局范围内乱扔对象;只有注册表本身是全局的。查找我从任何地方扔进注册表的任何东西都很方便,包括模型:
然而,就像单例一样,Registry 经常被人反对。这是一个 article by Brandon Savage with some thought about why not to use the Registry .反对注册表的主要论点是
那些投票反对注册表的人通常提倡使用 Dependency Injection ,尽管应该注意的是,一旦获得注册表实例,您也可以注入(inject)它。你没有 Inversion of Control然后,因为使用对象将从注册表中提取它需要的东西。不过,使用注册表作为服务定位器是一种有效的方法。
查看此 article by Martin Fowler about ServiceLocator vs. Dependency Injection .
就像在对您的问题的评论中指出的那样,Zend_Registry
不是严格的单例。除了使用通过 Zend_Registry::getInstance()
获得的全局实例之外,您还可以在需要的地方实例化多个实例。所以对象可以有自己的注册表。但是当以这种方式使用 Registry 时,它基本上只是一个美化的 ArrayObject。
最后说明:就像所有设计模式一样,如果适用于您的问题,请使用它。
关于php - Zend_Registry : real life examples,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2531168/
我有一个 Zend MVC 应用程序。我之前在应用程序中为 Zend Registry 设置了其他变量,它运行得非常好。然而这一次它不起作用。当我调用操作 setmanuel 时,脚本会回显测试。但是
假设我有一个这样的 Php 函数: private function verification($isXLSFile = false) { if ($isXLSFile) {
你找到Zend_Registry了吗?有用吗? 它应该用于哪些任务?哪个不是? 变量的全局状态不是一个好习惯。主要对象可以通过 $front->setParam('paramName', $objec
嘿,我想知道在应用程序中访问 Zend_Registry 时是否需要包含 getInstance() ,如果需要,为什么? 例如 Zend_Registry::getInstance()->g
我正在测试我的 Zend Framework 应用程序,并想测试当注册表中未设置特定键时会发生什么。这是我正在测试的功能: protected function getDomainFromConfig
你可以传递任何东西给Zend_Registry::set('myWidget', $someWidget), so that it's available later on . 但是,当您在别处检索它
我是一名优秀的程序员,十分优秀!