PHP - Site Wide Values
Posted on 2012-03-29
Firstly, I make use of .ini files on the server (located out of public reach) and read these with my PHP scripts. The main reason is to get the database parameters for that site so that I can connect.
Sometimes though, there are sites where there are several settings that need maintaining and for these, I put some in the ini file but store the rest on the database. These parameters are then used throughout the site to tailor what the user sees.
I tend to write my sites using the MVC design pattern - having a main controller (usually index.php) that does all the necessary pre and post amble and then the rest of its role is purely to send you off to the correct scripts (MVC) for the page in question.
The vast majority of the sites are written using PHP classes and some of these are common across many sites - for example, I have a login/logout class and an SQL abstraction class. The classes are tailored to the site via site specific css.
All of which I am sure is fairly standard for most people.
My question concerns the storage of the site parameters which I often want available to every class in the site - I'm sure I could be more restrictive, but it is easier to store them all in an array and make that available.
To my mind (knowledge) there are three options (maybe you know of more!) to do this:
1. Standard array that you pass to the class constructor - or to each function
2. Global array using the global keyword in each class/function
3. The $_session array - automatically available for all
So, what do people think? Firstly, do you agree that the concept is a valid one or are there better ways to achieve the requirement? Secondly, which of the above gets your vote - or something else - as the mechanism to implement the requirement?
Just for clarities sake - I guess there are two questions here! One about the use of .ini files to provide the pre-database connection parameters and then once you have your parameters (from either .ini or the database) how to pass them around the site.
I should perhaps say that if an error is detected then my error and exception handlers log these and also dump the entire array of site settings in force at that time to the error log so I can see what was going on at the time.