PHP - Application config file stored as - ini,php,sql,cached,php class,JSON,php array?

后端 未结 10 788
死守一世寂寞
死守一世寂寞 2020-12-13 14:15

I am trying to decide on the best way to store my applications configuration settings. There are so many options.

The majority of applications I have seen have used

相关标签:
10条回答
  • 2020-12-13 14:56

    It is best to do any core configuration in PHP itself, but if you are using a database and don't mind the extra overhead - you may find some flexibility in storing some settings in the database with a single extra query (assuming you organize it properly).

    Either way, storing this data in JSON, INI, XL, etc is just another unneeded abstraction that is done way too much nowadays on the web. Your best bet is pure PHP, unless you like the flexibility of some settings being in the database.

    0 讨论(0)
  • 2020-12-13 14:59

    In my view good solution would be ini files.

    I don't prefer config file using arrays/variables for storing settings; here is why:

    What if a user accidently re-named your setting variable?
    What if a variable with similar name is defined elsewhere too by the user?
    Variables in config file may be overwritten some where deep in the script or even included files.
    and possibly more problems....

    I like to use ini file for setting of my php apps. Here is why:

    It is section based
    It is easier
    You can set values by friendly names
    You don't have to worry about variables being overwritten because there are no ones.
    No conflict of variables of course. It allows more flexibility in specifying the types of values.

    Note: You need to use parse_ini_file function to read ini files.

    0 讨论(0)
  • 2020-12-13 15:04

    I like the idea of having "namespaces" or some kind of tree

    so you can have:

    db.default.user
    

    or

    db.readonly.user
    

    and so on.

    now regarding code what I did was an interface for the config readers: so you can have a memory reader, array reader, db reader, etc.

    and a config class that uses those readers and allow you have a config from any kind of source

    0 讨论(0)
  • 2020-12-13 15:07

    The best thing you can do is the simplest thing that could possibly work (php variables) and wrap it up in a class. That way you can change the implementation later without changing any client code. Create an interface that the configuration class implements and make the client code use the interface methods. If you later decide to store configuration in a database or JSON or whatever, you can simply swap out the existing implementation with a new one. Make sure your configuration class is testable and write unit tests.

    0 讨论(0)
提交回复
热议问题