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

After a few years in PHP development, I saw and heard various ways of storing "global area data" (global, constant, ini / XML / YML files, databases, singleton properties ...).

By "global area data" I mean:

  • Global application / project settings, such as
    • Database configuration
    • SMTP, FTP options
  • Database identifiers (for example, primary key values ​​for specific languages ​​or countries defined in the database)
  • Global runtime parameters such as
    • Enable Logging / Debugging
    • Environment - dev / test / prod
  • and etc.

... which should not change after extraction and should be easily accessible in any part of the project code.

Some global data can be stored as an associative array (therefore, it cannot be declared permanent).
For example: date formats for each language. By the way, I saw this other SO question about array constants, but isn't there something more readable than using unserialize wherever a constant array value is required?

My main question is: how would you recommend correctly storing (I mean clean, readable, reliable) data from the global area and why (pros / cons)?

Thanks.

+6
source share
4 answers

You can look at Zend_Config for the most commonly used configuration implementations.

  • array (php only, immediate but scattered and hard to read)
  • ini (easy to read and write by hand)
  • xml (detailed and difficult to process, but very flexible)
  • json (pretty easy to read, it might be great if you want to access it directly via js)
  • yaml (you basically write a serialized array)

Of course, an array may seem like the most immediate and uncomplicated solution, since it is pure PHP and does not need a special parser or writer.

Other formats, on the other hand, also have clear advantages. The Zend_Config documentation writes, for example, ini files.

The INI format is specialized for providing both the ability to have a hierarchy of configuration data keys and inheritance between data configuration. Hierarchy configuration data is supported by dividing keys by point or (".").

Using constants is not a good idea, because:

  • Your application does not have to see all your configuration options all the time and
  • more importantly, you cannot nest constants, and nesting is something really important for the configuration.
+2
source

In my opinion, the best way to manage any configuration is with INI files.

eg. I create one configuration.ini file that stores my entire system configuration, such as database information, URLs, etc.

 host_name="localhost"; database_name="my_database"; database_user="root"; 

While reading, you just need to parse this ini file in php using the php default function,

 $configuration = parse_ini_file("path/to/configuration.ini"); 
+2
source

You can save the settings no matter which you prefer. I partially relate to PHP arrays or INI files.

Once you do this, enter an access class that is available worldwide. You can do this singleton if you want, but not necessarily.

This class will analyze the settings store and create an internal data structure. Just make sure you don't have any setters so that the data cannot be overridden. See 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 access class is available worldwide, so you can get the settings anytime.

+2
source

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 INI file (first lines in the file) helps protect it:

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

Source: https://habr.com/ru/post/886469/


All Articles