Recommended way to manage global scope data and settings in PHP?

You can look at Zend_Config for the most frequent implementations of config array (php only, immediate but scattered and harder to read) ini (easy to read and write by hand) xml (verbose and harder to handle but very flexible) json (pretty easy to read, can be great if you want to access it directly via js too) yaml (you write directly a serialized array basically) Of course array may seem the most immediate and uncomplicated solution since it's pure PHP and doesn't need any special parser or writer On the other hand the other formats have clear advantages too. The Zend_Config documentation writes for example about ini files The INI format is specialized to provide both the ability to have a hierarchy of configuration data keys and inheritance between configuration data sections. Configuration data hierarchies are supported by separating the keys with the dot or period character (".") Using constants is not a good idea because: your application doesn't need to see all your config options all of the time and more importantly you cannot nest constants and nesting is something really important for configuration.

You can look at Zend_Config for the most frequent implementations of config. Array (php only, immediate but scattered and harder to read) ini (easy to read and write by hand) xml (verbose and harder to handle but very flexible) json (pretty easy to read, can be great if you want to access it directly via js too) yaml (you write directly a serialized array basically) Of course array may seem the most immediate and uncomplicated solution since it's pure PHP and doesn't need any special parser or writer. On the other hand the other formats have clear advantages too.

The Zend_Config documentation writes for example about ini files. The INI format is specialized to provide both the ability to have a hierarchy of configuration data keys and inheritance between configuration data sections. Configuration data hierarchies are supported by separating the keys with the dot or period character (".").

Using constants is not a good idea because: your application doesn't need to see all your config options all of the time and more importantly you cannot nest constants and nesting is something really important for configuration.

Zend_Config_Ini seems somewhat useless. It's just a bloaty wrapper around parse_ini_file and array_merge. And if it could actually be used to update the settings file (more frequent topic on SO), it probably would kill the user comments still.

– mario Apr 22 at 13:23 There is also Zend_Config_Writer, including Zend_Config_Writer_Ini! Framework.zend. Com/manual/en/… – markus Apr 22 at 13:25 And I wouldn't call that wrapper bloated.

Especially because it provides section functionality too. – markus Apr 22 at 13:27 Hmm, as expected. It strips .

Ini comments. -- As for the section functionality, that's already supported by parse_ini_file. I'm still confused what the advantage is.

– mario Apr 22 at 13:35 1 The advantage is, that you can exchange the adapter any time without changing anything in your code. That's what frameworks do, they wrap things in order to unify interfaces. If there was only ini and I was sure I would always use ini, I would agree with you that you could use parse_ini_file and array_merge and be done with it.

– markus Apr 22 at 13:42.

As my opinion, the best way to manage all configuration using INI files. E.g. I am creating one configuration.

Ini file, that stores all my system configuration like, database information, url etc... host_name="localhost"; database_name="my_database"; database_user="root"; On reading time, you just need to parse this ini file in php using php default function, $configuration = parse_ini_file("path/to/configuration. Ini").

Surely that is faster and does exactly the same thing. – B Tyler Apr 22 at 12:32 Nowadays, all clients want that they can change configuration on their end.So if we using php variables as file all times, that make confusing client all times. So INI file is the only way to make simpler for people who have no sound of php.

– Sanjay Mohnani Apr 22 at 12:36 1 I'm sorry to say this, but it's a terrible idea. There's absolutely no reason whatsoever to use INI files and then parse them if you can construct an array or define constants and include them in your project. You're just adding an unneccessery step of parsing in the process.

– Michael J.V.Apr 22 at 12:37 @B Tyler Using constants seems a good idea only at the first glance. If you think about what this would mean in terms of scope, you would realize that it is a very bad idea. You would make the configuration options accessible directly from everywhere in your software which is bound to create a maintenance nightmare.

There is no good reasons why the whole application should know all config options, you need to hide the config and then provide access to it if needed only through a dedicated handler like for example a config class. – markus Apr 22 at 12:57 1 Another reason why constants are not suitable for configuration is that they're not meant to be nested! Nesting is extremely important for configuration.

– markus Apr 22 at 13:23.

You can store the setting whichever way you prefer. I'm partial to PHP arrays or INI files. Once you have that, code an accessor class that is globally available.

You can make it singleton if you want, but not really required. This class will parse your settings store and create an internal data structure. Just make sure that you don't have any setters in there so that the data cannot be overridden.

Have a look at how Zend implements its Zend_Config class. This is what I'm talking about : URL1 Make sure your accessor class is available globally so you can get at the settings any time you want.

Parse_ini_file ; This is a sample configuration file ; Comments start with ';', as in php. Ini first_section one = 1 five = 5 animal = BIRD second_section path = "/usr/local/bin" URL = "example.com/~username" ; This is an array third_section phpversion = "5.0" phpversion = "5.1" phpversion = "5.2" phpversion = "5.3" Also adding this to the INI file (first lines in the file) helps keep it secure: ;404 Not FoundThe requested resource could not be found. ");?

> ;Secure INI file.

– JohnP Apr 22 at 13:58 @JohnP, because it's useless. You parse a file to get an array of values instead of defining it already as an array... – Charliepiga Apr 22 at 13:59 @Charliepiga you realize you can't actually define() arrays right? Define() only supports scalar.

Also, unlike a multidimensional array INI files are much easier to read and they cascade well – JohnP Apr 22 at 14:03.

(a) PHP operates like most other CGI implementations. When the webserver receives a request for a specific URL that happens to be a . Php script, then the interpreter is invoked with the referenced script.

The PHP interpreter runs it, and sends the output back to the webserver/client. After it's finished, that CGI interpreter terminates and takes all its runtime data with it. That explains why variables do not persist or are shared between two distinct PHP scripts or invocations.

It works the same for the ordinary mod_php handler. Only there it's simple process forking (duplication) and termination. (b) The $_SESSION array is handled by PHP.

It can discern the per-user storage using the unique cookie key. And since each CGI or mod_php process does have a separate variable pool, there is really no issue. PHP just needs a bit of file locking to prevent two scripts from overwriting the session store when the same user requested two scripts at once.

(c) "Global" variables also only exist per-process. They are gone like everything else when the PHP script has finished. (d) That's two different concepts.

I see what you mean with the usage similarities. But globals are just a shared scope, more closely circumscribed as value object maybe. You can access both using a global identifier, but that's about the similarities.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions