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

Class Settings {
        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:

function get($var) {
        $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.

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