Creating a PHP Settings Class
(Page 3 out of 3)XML Settings Class
Like the previous two classes, let's start with an empty class again:
}
Since we are going to use an XML file as a config file now, we will have to use an XML parser. Since XML parsers are fairly standard stuff, I'm just going to use the XML Lib by Keith Devens, available at http://keithdevens.com/software/phpxml.
The load() method of our XML class will have to parse an XML config file that looks like this:
The following code will do exactly what we want:
if (file_exists($file) == false) { return false; }
include ('xmllib.php');
$xml = file_get_contents($file);
$data = XML_unserialize($xml);
$this->_settings = $data['settings'];
}
In the above example we use the XML library to unserialize the XML (changing it into a normal PHP array), and then get the settings from the unserialized XML.
Again, to use the XML class, all we have to do is change one line:
$settings = New Settings_XML;
$settings->load('config.xml');
echo 'XML: ' . $settings->get('db.host') . '';
Just like the previous two times everything is exactly the same, except for the first line.
Now let's create the final config format: YAML.
YAML Settings Class
YAML is a relatively new data format, and uses mostly white-space to define data structures. More information can be found at http://www.yaml.org/, but we'll just be using the Spyc library to parse YAML files into PHP structures.
Let's start with the empty class:
}
The load() method now needs to parse a YAML config file, which looks like this:
name: test
host: localhost
(Note that the indention is very important.)
With the Spyc library parsing YAML is no problem, and it takes only three lines to import the settings:
if (file_exists($file) == false) { return false; }
include ('spyc.php');
$this->_settings = Spyc::YAMLLoad($file);
}
As you can see we used the Spyc library to parse the YAML, and then passed the results to the $_settings array.
You can probably guess how to use the YAML settings class, and all we need to do again is change only one single line.
Conclusion
In this tutorial I've shown you how to create a Settings class that can handle four different formats, but the great thing about our Settings class is that it's extremely flexible. If you're halfway through your project, and suddenly want to start using a different format it's no problem. All you have to do is change a single line, and the rest of your code is still good to go.
And that's the main point of this tutorial. We've effectively decoupled our Settings class from the rest of the project. Our settings class doesn't need to know about the rest of the script, and the rest of the script doesn't need to know which Settings class we're using, as long as there is a load() and get() method. This is called 'polymorphism', and can be a very powerful tool.
If you want to have a look at the full Settings Class, click here to download it.
If you have any questions or comments on this tutorial, feel free to leave them below. Also join us at PHPit Forums!
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!