PHP Zend Framework-Zend_Config和全局状态 [英] PHP Zend Framework - Zend_Config and global state

查看:77
本文介绍了PHP Zend Framework-Zend_Config和全局状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相对于使用简单的常量文件,我正在评估Zend_Config_Ini的优势。

I'm in the process of evaluating the benefits of Zend_Config_Ini versus using a simple constant file.

例如-

define('DB_HOST',localhost);

//versus

$config = new Zend_Config_Ini('/path/to/config.ini', 'staging');
echo $config->database->params->host;   // prints "dev.example.com"

唯一的是$ config不是全局的无障碍。因此,您需要使用Zend_Registry存储应用程序的使用情况,而不必每次都初始化。

The only thing is that the $config is not globally accessible. So then you need to use Zend_Registry to store for application usage, without having to initiate each time.

这似乎增加了不必要的复杂性....我是否缺少某些东西?或者说Zend_Config + Zend_Registry是一种随着应用程序的发展而长期发展起来更好的技术?

This seems to add more complexity than needed.... am I missing something or is Zend_Config + Zend_Registry a technique that is better in the long run as an app grows?

推荐答案

将注册表与配置一起使用的主要优点是避免污染全局名称空间。假设您要包含第三方库,并且您的应用程序和库都定义了一个常量DB_HOST。

The main advantage of using the Registry with a config is to avoid polluting the global namespace. Say, you want to include a third party lib and your app and the lib both define a constant DB_HOST. No good.

此外,Zend Framework中的许多工厂都使用config对象创建实例。 Zend_Db 是一个很好的例子。您只需将其传递给 $ this->数据库,它将花费实例化适配器所需的内容。

In addition, many of the factories in Zend Framework utilize the config object to create instances. Zend_Db is a good example of this. You just pass it $this->database and it will take what it needs to instantiate your adapter.

您还可以使用自定义功能扩展注册表,例如查找器方法或类似的东西。检查Fowler的模式描述

You can also extend the registry with custom functionality, e.g. finder methods or stuff like that. Check the pattern description by Fowler.

这篇关于PHP Zend Framework-Zend_Config和全局状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆