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

Class Settings_PHP Extends Settings {

}

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:

function load ($file) {
        if (file_exists($file) == false) { return false; }

}

Then we need to include the config file, using the standard include() function, like this:

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

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

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

Class Settings_INI Extends Settings {

}

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:

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

[db]
name = test
host = localhost

Using the INI class is almost exactly the same as using the PHP class:

// Load settings (INI)
$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.

« Previous: Introduction & Creating Base Class
Next: XML & YAML 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