简体   繁体   English

PHP Zend框架 - Zend_Config和全局状态

[英]PHP Zend Framework - Zend_Config and global state

I'm in the process of evaluating the benefits of Zend_Config_Ini versus using a simple constant file. 我正在评估Zend_Config_Ini与使用简单常量文件的好处。

eg - 例如 -

define('DB_HOST',localhost);

//versus

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

The only thing is that the $config is not globally accessible. 唯一的问题是$ config不是全局可访问的。 So then you need to use Zend_Registry to store for application usage, without having to initiate each time. 因此,您需要使用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? 这似乎增加了比需要更多的复杂性....我错过了什么,或者Zend_Config + Zend_Registry是一种技术,从长远来看,随着应用程序的增长更好?

The main advantage of using the Registry with a config is to avoid polluting the global namespace. 将Registry与配置一起使用的主要优点是避免污染全局命名空间。 Say, you want to include a third party lib and your app and the lib both define a constant DB_HOST. 比如说,你想要包含第三方lib,你的app和lib都定义了一个常量DB_HOST。 No good. 不好。

In addition, many of the factories in Zend Framework utilize the config object to create instances. 此外,Zend Framework中的许多工厂都使用config对象来创建实例。 Zend_Db is a good example of this. Zend_Db就是一个很好的例子。 You just pass it $this->database and it will take what it needs to instantiate your adapter. 你只需传递$this->database ,它将需要实例化你的适配器。

You can also extend the registry with custom functionality, eg finder methods or stuff like that. 您还可以使用自定义功能扩展注册表,例如查找器方法或类似的东西。 Check the pattern description by Fowler. 检查Fowler的模式描述

A nice advantage of Zend_Config is that you don't depend on a "PHP source code file" ; Zend_Config一个很好的优点是你不依赖于“PHP源代码文件”; just a .ini file will do -- and some prefer modifying an .ini file instead of a PHP one. 只需一个.ini文件 - 有些人更喜欢修改.ini文件而不是PHP文件。

And it's easier to modify an .ini / XML file programatically - there are classes / functions to do that ! 并且以编程方式修改.ini / XML文件更容易 - 有类/函数可以做到这一点!

With .ini files and Zend_Config , you also have nice functionnalities already provided ; 使用.ini文件和Zend_Config ,您还可以提供良好的功能; for instance, inheritance between sections (ie, you can have a "generic" section, and "staging" and "production" that overwrite some values) 例如,部分之间的继承(即,您可以拥有“通用”部分,以及覆盖某些值的“暂存”和“生产”)


A thing that can be insteresting about Zend_Config , too, is consitency : Zend Framework, with Zend_Application , already supposes you'll have one configuration file ; 关于Zend_Config也可能有点兴趣的事情就是使用:Zend Framework, Zend_Application ,已经假设你将拥有一个配置文件; so, why not a second one ? 那么,为什么不是第二个呢? Or even re-use the first one ? 甚至重复使用第一个?

And it's the same with several classes of the Framework, which can work or be configured with an instance of Zend_Config ; 并且它与Framework的几个类相同,它可以使用Zend_Config的实例工作或配置; if you already have that one, it suddenly becomes easier ;-) 如果你已经拥有那个,它会突然变得容易;-)


If I had to choose between a PHP file with defines and a .ini file, for things that are configurable, I would probably go with the .ini file (And Zend_Config + Zend_Registry when needed) . 如果我必须在带有define和.ini文件的PHP文件之间进行选择,对于可配置的东西,我可能会使用.ini文件(需要时使用Zend_Config + Zend_Registry

Yes, you're correct, using the Zend sanctioned method for configuration is more complex than defining a series of global constants. 是的,你是对的,使用Zend认可的配置方法比定义一系列全局常量更复杂。 The advantages you get are 你获得的好处是

No Global Namespace Collisions 没有全局命名空间冲突

If you ever need to integrate your application with another project, having global constants represent your application config may cause problems. 如果您需要将应用程序与其他项目集成,则使用全局常量表示应用程序配置可能会导致问题。 What are the chances another project has a constant named DB_HOST ? 另一个项目有一个名为DB_HOST的常量的可能性有DB_HOST How can a developer who's using your hybrid system tell which constants are the config for your application vs. the integrated application? 使用混合系统的开发人员如何知道哪些常量是应用程序与集成应用程序的配置?

Building on the Work of Others 以他人的工作为基础

So, you have a single file with all your config values. 因此,您有一个包含所有配置值的文件。 How are you going to deploy that into different environments? 你打算如何将它部署到不同的环境中? (production, staging, etc.) Chances are you're a smart person who could come up with a solution, but how is another developer coming into your project going to know how that solution works? (生产,升级等)你很可能是一个能够提出解决方案的聪明人,但是另一个开发人员如何进入你的项目会知道该解决方案是如何工作的? How are other modules in your application going to know how to read your global config? 您的应用程序中的其他模块如何知道如何读取全局配置?

Zend_Config has already "solved" many of the problems. Zend_Config已经“解决”了许多问题。 By taking on a bit more complexity and abstraction up-front you get a known path forward, and can spend more time on solving the problems of your application instead of inventing and supporting Yet Another Configuration System. 通过预先考虑更多的复杂性和抽象,您可以获得前进的已知路径,并且可以花更多的时间来解决应用程序的问题,而不是发明和支持Yet Another Configuration System。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM