Creating a PHP Settings Class
(Page 1 out of 3)Abstract
In this tutorial you will be shown how to create a settings class which can handle different formats (PHP, INI, XML and YAML), using polymorphism
Introduction
A config.php or a settings.xml file is a very common thing for most PHP scripts, and it's usually where all the script settings (e.g. database information) are stored. The easiest way is to simply use a simple PHP script as a config file, but this may not be the best way, and it's certainly not the most user-friendly way.
In this tutorial we'll have a look at creating a Settings class which can handle any type of config format. I'll take you through the steps necessary to handle four different formats (PHP, INI, XML and YAML), but it's very easy to add more formats.
Let's get started.
Writing the base class
Because our Settings class needs to support multiple formats it's best to write a base class first, and then have separate specific classes. In PHP 5 we would make this base class an abstract class, but since we're using PHP4, it'll just be a normal class.
The basic skeleton of our base class looks like this:
var $_settings = array();
function get($var) {
trigger_error ('Not yet implemented', E_USER_ERROR);
}
function load() {
trigger_error ('Not yet implemented', E_USER_ERROR);
}
}
As you can see we've defined two methods: get and load. The get() method is used to get the value of a certain setting, and the load() method is used to load the settings. We've also defined a single property, $_settings, which will hold the loaded settings.
We can't implement the load() method yet, because each format requires a different way of loading. But we can implement the get() method already, so let's do that. See the example below:
$var = explode('.', $var);
$result = $this->_settings;
foreach ($var as $key) {
if (!isset($result[$key])) { return false; }
$result = $result[$key];
}
return $result;
}
The get() method first splits the passed varname into separate parts. This makes it possible to have multidimensional settings, for example db.name, db.host, etc.
Now that we've got a working get() method, all we need to do is implement the load() method, but we're not going to do that in the base class. We'll use separate child classes for that.
Let's start with the first one, the PHP settings class.
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!