Creating a PHP Settings Class
(Page 2 out of 3)PHP Settings Class
The PHP settings class is a child class of the base settings class, and therefore needs to extend the base class, like this:
}
Since we are inheriting from the base class, the get() method already works, so all we need to do now is write the load() method. What the load() method needs to do is include the config file, and get the necessary settings into the $_settings array. The config.php file looks like this:
$db = array();
// Enter your database name here:
$db['name'] = 'test';
// Enter the hostname of your MySQL server:
$db['host'] = 'localhost';
?>
The first thing the load() method needs to do is check whether the config file exists, like this:
if (file_exists($file) == false) { return false; }
}
Then we need to include the config file, using the standard include() function, like this:
if (file_exists($file) == false) { return false; }
// Include file
include ($file);
}
Now that the config file has been included, the config variables have also been included. But we still need to get the config variables into the $_settings array. This can be done with the get_defined_vars() function, which returns an associative array of all the variables that exist in the current scope.
The following piece of code will import the config variables into the $_settings array:
unset($file);
$vars = get_defined_vars();
// Add to settings array
foreach ($vars as $key => $val) {
if ($key == 'this') continue;
$this->_settings[$key] = $val;
}
The above code first gets all the defined vars, and then imports them in the $_settings array. We make sure to skip on the $this variable, since it refers to the object (and therefore it's not a config variable).
Our complete load() method now looks like this:
if (file_exists($file) == false) { return false; }
// Include file
include ($file);
// Get declared variables
unset($file);
$vars = get_defined_vars();
// Add to settings array
foreach ($vars as $key => $val) {
if ($key == 'this') continue;
$this->_settings[$key] = $val;
}
}
The following code demonstrates the use of the Settings_PHP class:
include ('settings.php');
// Load settings (PHP)
$settings = new Settings_PHP;
$settings->load('config.php');
echo 'PHP: ' . $settings->get('db.host') . '';
?>
Now let's take a look at using an INI file as a config file.
INI Settings Class
Just like with the PHP settings class, let's start with an empty INI class that extends the base class:
}
Now we need to create the load() method again, but this time we'll have to create a load() method that can read INI files. The following will do the trick:
if (file_exists($file) == false) { return false; }
$this->_settings = parse_ini_file ($file, true);
}
In the above code snippet we use the parse_ini_file() to read in our config.ini file, which looks like this:
name = test
host = localhost
Using the INI class is almost exactly the same as using the PHP class:
$settings = new Settings_INI;
$settings->load('config.ini');
echo 'INI: ' . $settings->get('db.host') . '';
As you can see all we needed to change was the first line, and this makes changing between different setting formats extremely flexible. You don't have to change any code in your script, except for one line.
Let's create the XML settings class now.
June 22nd, 2006 at 4:20 pm
[…] http://phpit.net/article/create-settings-class-php/ […]
July 7th, 2006 at 3:31 pm
Very nice… I’ll be looking back at this later on, keep the tutes coming :)
July 10th, 2006 at 3:32 pm
I think that the cost of parsing a configuration file is to expensive for php, how is it in realty ?
July 13th, 2006 at 6:02 am
so if I have a database class which handles database queries, I would need to include the settings class in my database class, which means that my database class will be relying on settings class, is that good or bad?
July 26th, 2006 at 4:12 am
[…] Great Class Article (#1 on google) Older article, but helps explain why to use classes Simple php class overview Advanced Article from Zend (the creators of the Zend PHP Engine) on classes Beginners tutorial to classes Abstract way of thinking about OOP An extensive look at OOP in php4 Advanced overview of OOP coding strategy Using advanced concepts with PHP classes Creating a settings class in PHP By far the best overall introduction to OOP I have EVER come across: The best OOP and PHP class tutorial! Happy classing! […]
August 22nd, 2006 at 9:45 pm
newbie herer .. i like this approach…
will try to incorporate…thnx!