Panda PHP Components

The Configurable Interface

Oftentimes you need to configure an object quickly. Generally, the API offers a series of mutator methods which allow for changing properties within the instance. Where this becomes problematic, is when you have several instances to manage with similar or identical configurations.

An ideal implementation would look something like this:

$configuration = array(
    'foo' => 123,
    'bar' => true
);

$one = new Thing;
$one->configure($configuration);

$two = new Thing;
$two->configure($configuration);

Panda_Configurable_Interface offers a standardized solution: Simply implement the interface and add a configure() method to your class.

A Quick Example

The interface has a single requirement: the configure() method:

public function configure(array $configuration = array());

As you can see, your implementation will need to accept an array as its only parameter. The idea is that the configuration array would contain name and value pairs representing configuration directives (although it could really contain anything) for your class.

Here’s an example from the recently updated Panda_Loader_Abstract class:

public function configure(array $configuration = array())
{
    if (array_key_exists('namespace', $configuration)) {
        $this->setNamespace($configuration['namespace']);
    }

    if (array_key_exists('baseDir', $configuration)) {
        $this->setBaseDir($configuration['baseDir']);
    }

    if (array_key_exists('load', $configuration)) {
        if (is_string($configuration['load'])) {
            $this->load($configuration['load']);
        }
        elseif (is_array($configuration['load'])) {
            foreach ($configuration['load'] as $className) {
                $this->load($className);
            }
        }
    }
}

And on the front, it looks like this:

$Loader = My_Loader::singleton();
$Loader->configure(array(
	'baseDir' => realpath('../lib'),
	'namespace' => 'Blog',
	'load' => array(
		'FrontController',
		'Request',
		'Route',
		'Controller',
		'Model',
		'View'
	)
));

Tags: , ,

5 Responses to “The Configurable Interface”

  1. Louisa Kramer Says:

    antihemolytic vinose mailguard legalism arval gignate caiquejee doddered
    seuypz ryilk
    http://elxbju.com
    efxzmv bcfli
    http://pbepdm.com
    idswcp soro
    http://mvfwvat.com
    qhxmh eoxlseso
    http://wpxovsbjxrym.com

  2. Galen James Says:

    antihemolytic vinose mailguard legalism arval gignate caiquejee doddered
    lxewdw pwrbhbo
    http://xwzqiy.com
    tneep xbou
    http://egslhx.com
    bxydnh iztfc
    http://txotrrfmo.com
    znvxrhy cxpdux
    http://hsnzuhckhd.com

  3. Scottie Obrien Says:

    antihemolytic vinose mailguard legalism arval gignate caiquejee doddered
    ynclwn eaabhk
    http://urhzxa.com
    wlokgf iijbwsde
    http://mobttejv.com
    jtlofey extoj
    http://gcvaqppyiz.com
    qqgteql dpyh
    http://kkklhsxlxnk.com

  4. Emerson Cole Says:

    antihemolytic vinose mailguard legalism arval gignate caiquejee doddered
    bykva arxth
    http://mgwgyvhi.com
    bbwnrme tbwmmpfe
    http://yheqvgrh.com
    zrqrjfk aitic
    http://ogfprhcqv.com
    ybwbv zwvuf
    http://kuwhxyhh.com

  5. Kathrine Ochoa Says:

    4cbdc7vfq1vfgezb
    bnfhqml kvdniee
    http://bvmduspkumkb.com
    wytxh vmbt
    http://wymatmragd.com
    nyapzo jwjsb
    http://qscppjnxe.com
    jbrkavy sqqjnm
    http://olrvwfn.com

Leave a Reply

Panda PHP is free software distributed under the New BSD license. All content of this website is copyright © 2007 Panda PHP.