开发者

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

After a few years in PHP development, I sa开发者_StackOverflow社区w and heard various ways for storing "global scope data" (globals, constants, ini/XML/YML files, database, singleton properties...).

By "global scope data", I mean:

  • Global application/project settings such as
    • Database configuration
    • SMTP, FTP parameters
  • Database identifiers (e.g. primary key values for specific languages or countries defined in DB)
  • Global runtime settings such as
    • Enable logging / debug
    • Environment is dev / test / prod
  • etc.

... which are not supposed to change once retrieved, and need to be easily reachable in any part of the project code.

Some global data may need to be stored as associative array (so cannot be declared as constant).

For example: date formats per language. BTW, I saw this other SO question about array constants, but isn't there something more readable than using unserialize everywhere an array constant value is needed?

My main question is: what is the way you would recommend to store properly (I mean clean, readable, reliable) global scope data, and why (pros/cons)?

Thanks.


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:

  1. your application doesn't need to see all your config options all of the time and
  2. more importantly you cannot nest constants and nesting is something really important for configuration.


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");


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 : http://framework.zend.com/manual/en/zend.config.html

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 = "http://www.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:

;<?php die("<br /><br /><br /><br /><br /><h3>404 Not Found</h3><br /><br />The requested resource could not be found."); ?>
;Secure INI file
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜