ASPit - Totally ASP JSit - Totally JavaScript
Search PHPit

Use this textbox to search for articles on PHPit. Seperate keywords with a space.

Advertisements

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:

Class Settings_XML Extends Settings {

}

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:



       
                test
                localhost
       

The following code will do exactly what we want:

function load ($file) {
        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:

// Load settings (XML)
$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:

Class Settings_YAML Extends Settings {

}

The load() method now needs to parse a YAML config file, which looks like this:

db:
   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:

function load ($file) {
        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!

« Previous: Creating PHP & INI Settings Class



6 Responses to “Creating a PHP Settings Class”

  1. Andy Boyd » Creating a PHP Settings Class Says:

    […] http://phpit.net/article/create-settings-class-php/ […]

  2. R. Villar David Says:

    Very nice… I’ll be looking back at this later on, keep the tutes coming :)

  3. Gaetan Says:

    I think that the cost of parsing a configuration file is to expensive for php, how is it in realty ?

  4. Eric Lin Says:

    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?

  5. jud.io » Archive » PHP 101: 11 tutorials about classes in php Says:

    […] 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! […]

  6. Rodrigo D. Says:

    newbie herer .. i like this approach…
    will try to incorporate…thnx!

Leave a Reply

About the author
Dennis Pallett is the main contributor to PHPit. He owns several websites, including ASPit and Chill2Music. He is currently still studying.
Article Index
  1. Introduction & Creating Base Class
  2. Creating PHP & INI Settings Class
  3. XML & YAML Settings Class
Bookmark Article
Download Article
PDF
Download this article as a PDF file