Jump to content
xisto Community
BCD

How Do You Create Your Config File Parameters

How do you save config parameters?  

4 members have voted

You do not have permission to vote in this poll, or see the poll results. Please sign in or register to vote in this poll.

Recommended Posts

This question applies to other programming/scripting languages too, apart from PHP. But since I am more interested in the inputs from developers in PHP language I have posted under PHP category.When creating an application there are many settings/configuration which are conveniently assigned to variables, instead of hard coding them. These variables are commonly stored in a configuration file or may be in a database.So, my question is, how do you declare these parameters?1) Are the variables defined globally, so that it is accessible in any script simply including the config file?2) OR Do you create a static class to store all the variables, going the OOP way?Which method do you prefer and Why?Edit: Changed poll option to multivote.

Edited by BCD (see edit history)

Share this post


Link to post
Share on other sites

I use a few methods but since I can only vote one choice i pick "Store the variables globally in a config file".It is on a case to case basis and highly depends on the PHP version running on the server. As much as possible I use static class to limit the access to certain parts of the code and when I need to use global variables to store them, I load them up in an array. The config file that I use only contains base directory structure on where I will load up the other classes and a database connection information where I will load up the full configuration information for the scripts.

Share this post


Link to post
Share on other sites

The most often use for me to make a config file (which is my method) is for database connections. A site might have 6, 7, 25 pages that need to connect to the same database, at first i would write the code into each file to connect to the DB but then when uploading a site the database address or user etc... changes so i have to edit 5 or 6 pages each time so now i just make a separate PHP file called something like connect.php and use REQUIRE to pull it in to the pages. Usually it is something like:

<?//connect.php$host = "localhost";$user = "logmein";$pass = "please!";$db = "mydata";mysql_connect.....;mysql_select_db...$db...;?>

Simple and easy!

Share this post


Link to post
Share on other sites

I use a few methods but since I can only vote one choice i pick "Store the variables globally in a config file".
It is on a case to case basis and highly depends on the PHP version running on the server. As much as possible I use static class to limit the access to certain parts of the code and when I need to use global variables to store them, I load them up in an array. The config file that I use only contains base directory structure on where I will load up the other classes and a database connection information where I will load up the full configuration information for the scripts.


Did not think about multiple options, so modified the poll to vote multiple options now.

I too go by creating static class for config parameters. Any specific reason of using global variables to store and then later load them in an array instead of storing them into array upfront. Or is it just the convenience of making the config file clean and non-programmer readable. Because somtimes when creating scripts for clients, the clients want easy to modify config's.


The most often use for me to make a config file (which is my method) is for database connections. A site might have 6, 7, 25 pages that need to connect to the same database, at first i would write the code into each file to connect to the DB but then when uploading a site the database address or user etc... changes so i have to edit 5 or 6 pages each time so now i just make a separate PHP file called something like connect.php and use REQUIRE to pull it in to the pages. Usually it is something like:
Simple and easy!


Yes, I too find that the most common settings are database settings.




I wanted to talk about any other settings? How do you people manage debugging. Like, I create a flag in condig file called 'debug'. While working on the script I switch on the debug flag so that the appropriate debug statements spread throughout the script plunge into action if an error is to occur.

Do you use such kind of debugging config flags in config files? Or do you take advantage of the built-in features in editor/IDE? If so, which one do you use?

I read this article on PHP configuration on IBM developers site: PHP Configuration Patterns
The article lists several methods of storing configuration settings like INI, plain PHP, txt files, and even xml files. I was perplexed seeing the various methods apart from the plain php method. The text file and XML method was interesting as its advantageous for writing settings to file. But if its just reading the config settings, the plain php is the best I can think of.

In the PHP method described in that article, the author talks about the variable declared in the code as a constant (Listing 4):
<?php# Specify the location of the temporary directory#$TEMPLATE_DIRECTORY = "tempdir";?>
Does the variable need to be prefixed with a define or const? I liked the idea of getter like function used there instead of just variables. But, instead of that, isn't it more easier to just define them as constants? I guess the global is used so as to avoid unnecessary variable passing through the functions.

Share this post


Link to post
Share on other sites

For settings that the public can safely look at, i store in a file that uses the INI format. I have a class that i made that saves to this format; i don't need an INI parser since PHP provides one for me, but i couldn't find one that converts arrays into INI-formatted text in PHP, so i made my own. If my script deals with a database, then i'll store valuable, private information in there. If it doesn't use a database, then i'll store that information statically within my script's controller if needed.

I do not save any debugging values within any configuration. Instead i have a static method handle that for me, perhaps with the name enableDebugging(). The user doesn't need to be bothered with debugging messages, especially since it is the job of the developer to ensure code stability for their script. The only errors the user should be bothered with are those that are not caused by the script or errors that are out of the script's control. Using a static method instead of storing any value within the configuration helps limit things more to developers. I would much rather include "forgiveness" within my script than to perhaps confuse the user with errors they need not worry about.

Share this post


Link to post
Share on other sites

I save the configuration values in an INI file which I read and store the values in constants, that they couldn't be modified with some exceptions of course, it's all done automatically for those options which are needed to start the execution.. All the other configuration is usually in a database, that you could easily change them from an admin panel..Configuration INI files usually includes connections to mysql, some default values which can be overridden by database values, just in case if mysql is down, usually you need some default values if you can't get them from the DB, for example the default template or default timezone or even default language of the site.

Share this post


Link to post
Share on other sites

It really annoys me when I see scripts people have made with the site configuration written in to the database. I know it shouldn't but I just see it as a waste of database resources and an unnecessary load on the database as well.

 

My site config always gets written in to a config.php file, usually in a lib/ directory. Also, making a nice little interface in the sites admin panel to easily configure and change the config is a bonus as well.

 

Creating an efficient site always requires knowing what actually needs inserting in to a database and what can be stored in a local flat file. Creating a database table to store values which will remain constant for all users is not recommended. An example of this would be standard site config such as the title of the website, or copyright owner of the website (More dynamic to put these values in to a variable which can be called on multiple pages than hard-coding it in pages then having to manually change them all later). There is no reason you can't just put this information in to a config file, and your database will not have to run through a query, select the information then extract it and organise it on every single page every single user loads. Instead you call two variables from a file, that's it.

 

To avoid database load on sites which have the potential to have hundreds or thousands of registered users I always try to avoid putting data in to the database that doesn't need to be there. Say you have your own PHP RPG game, there is a module system in place for admins to install and uninstall modules (additional content) from the game. Instead of creating a database table with the information needed for these modules, I create a module list file with the file permission 0666 (read/write). Here is an example:

 

$getaddon[] = "[link=boss][name=The Boss][priv=SuperAdminModUser]";	$getaddon[] = "[link=airport][name=Airport][priv=SuperAdminModUser]";	$getaddon[] = "[link=gym][name=Gym][priv=SuperAdminModUser]";

So now we have a php file called list.php with an array inside of different strings that we can loop through using a foreach() statement. This is similar to BBCode because it is an easy format to process using the preg_replace() function.

 

foreach($getaddon as $row){  $link = preg_replace('/\[link=(.*?)\]\[name=/is');  $name = preg_replace('/\[name=(.*?)\]\[priv=/is');  $priv = preg_replace('/\[priv=(.*?)\]/is');}

So now we have the link, name and user privelages inside a variable, but we need to do something with those variables before they get overwritten by the next loop of the foreach statement, so how about echoing an url to load the module.

 

foreach($getaddon as $row){  $link = preg_replace('/\[link=(.*?)\]\[name=/is');  $name = preg_replace('/\[name=(.*?)\]\[priv=/is');  $priv = preg_replace('/\[priv=(.*?)\]/is');  if (strpos($priv, $_SESSION['user']['type']) === true){    echo '<a href="http://forums.xisto.com/no_longer_exists/ />';  }}

So assuming you had a variable named $_SESSION['user']['type'] and it contains the string 'User', each link will be echoed on the page and look like this

 

The Boss

Airport

Gym

 

Now this method of calling data from a flat file is a lot more efficient than creating a database table like this,

 

CREATE TABLE `modules` (  `id` int(5) NOT NULL,  `name` varchar(20) NOT NULL,  `link` varchar(20) NOT NULL,  `priv` enum('Super', 'Admin', 'Mod', 'User') NOT NULL,  `installed` enum('Yes', 'No') NOT NULL,  PRIMARY KEY  (`id`));

So although it may be easier for you to call this information from the database using a mysql_query() statement and updating the information as well, in the long run it will start to drag down your database speed unnecessarily.

 

So that's a short description of why I prefer using config files instead of a database to store my sites config information lol

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.