I want to create a config file for my PHP project, but I'm not sure what the best way to do this is.
I have 2 ideas so far.
1-Use Variable
$config['hostname'] = "localhost";
$config['dbuser'] = "dbuser";
$config['dbpassword'] = "dbpassword";
$config['dbname'] = "dbname";
$config['sitetitle'] = "sitetitle";
2-Use Const
define('DB_NAME', 'test');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_HOST', 'localhost');
define('TITLE', 'sitetitle');
3-Use Database
I will be using the config in classes so I'm not sure which way would be the best or if there is a better way.
Well - it would be sort of difficult to store your database configuration data in a database - don't ya think?
But really, this is a pretty heavily opinionated question because any style works really and it's all a matter of preference. Personally, I'd go for a configuration variable rather than constants - generally because I don't like things in the global space unless necessary. None of the functions in my codebase should be able to easily access my database password (except my database connection logic) - so I'd use it there and then likely destroy it.
Edit: to answer your comment - none of the parsing mechanisms would be the fastest (ini, json, etc) - but they're also not the parts of your application that you'd really need to focus on optimizing since the speed difference would be negligible on such small files.
I use a slight evolution of @hugo_leonardo 's solution:
This allows you to use the object syntax when you include the php :
$configs->host
instead of$configs['host']
.Also, if your app has configs you need on the client side (like for an Angular app), you can have this
config.php
file contain all your configs (centralized in one file instead of one for JavaScript and one for PHP). The trick would then be to have another PHP file that wouldecho
only the client side info (to avoid showing info you don't want to show like database connection string). Call it sayget_app_info.php
:The above assuming your
config.php
contains anapp_info
parameter:So your database's info stays on the server side, but your app info is accessible from your JavaScript, with for example a
$http.get('get_app_info.php').then(...);
type of call.Define will make the constant available everywhere in your class without needing to use global, while the variable requires global in the class, I would use DEFINE. but again, if the db params should change during program execution you might want to stick with variable.
I normally end up creating a single conn.php file that has my database connections. Then i include that file in all files that require database queries.
If you think you'll be using more than 1 db for any reason, go with the variable because you'll be able to change one parameter to switch to an entirely different db. I.e. for testing , autobackup, etc.
You can create a config class witch static properties
then you can simple use it:
Sometimes in my projects I use a design pattern SINGLETON to access configuration data. It's very comfortable in use.
Why?
For example you have 2 data source in your project. And you can choose witch of them is enabled.
Somewhere in config file you choose:
When you change source whole app shoud switch to new data source, work fine and dont need change in code.
Example:
Config:
Singleton class:
... and somewhere in your code (eg. in some service class):
We can obtain an AppConfig object from any place in the system and always get the same copy (thanks to static). The init () method of the class is called In the constructor, which guarantees only one execution. Init() body checks The value of the config $dataSource, and create new object of specific data source class. Now our script can get object and operate on it, not knowing even which specific implementation actually exists.