LogoPhly, boy, phly
the weblog and site of Matthew Weier O'Phinney

Friday, May 19. 2006

Introducing Phly_Struct and Phly_Config

I often find myself needing a configuration module of some sort -- for storing application parameters, bootstrapping, template variables, what have you. I typically will either:

  1. Create a PHP file that creates and returns an array, and suck that in via include, or
  2. Create an INI file and suck it in via parse_ini_file, or
  3. Create an XML file and suck it in via SimpleXML.

The first method gives great flexibility of structure and types, but isn't portable to other languages (well, not easily; you could turn it into JSON, or serialize it, etc). The second method (INI files) is handy because the syntax is so concise, and can translate to other projects in other languages easily if necessary; however, you can only easily go two levels deep (using [sections] in the file). The third method is very portable, and allows nested structures -- but doesn't allow usage of many specific PHP types.

I find, however, that each has their place. The problem, however, is: once I bring them into my project, how can I access them? Better yet, would there be a way to bring in configurations of many types and still access them all in the same way?

Not happy with solutions out there, I did the only logical thing: I reinvented the wheel, and added some new tread of my own.

The solution started out as Phly_Config, which was going to be a generic storage container with several adapters for loading configurations. However, in talking with Paul, he noticed two tools emerging. To quote him:

  1. A general-purpose array/object/struct implementation.
  2. Config adapters to push data into the struct.

I got to thinking about it, and decided he was right -- and so Phly_Struct was born.

Now that I've hyped it up, just what do the tools offer? I think examples sum it up best. First, Phly_Struct:


$array = array(
    'key1 => 'value1',
    '
key2 => array(
        'subkey1' => array(
            'subsubkey1' => 'subsubvalue1',
            'subsubkey2' => 'subsubvalue2'
        ),
        'subkey2' => 'subvalue2'
    ),
    'key3' => 'value3'
);
$struct = new Phly_Struct($array);

echo 'The value of the second subkey is ' . $struct->key2->subkey2;
$struct->key3 = 'Some new value!';
$struct->key2->subkey1->subsubkey1 = 'I am a third level value';

// Let's loop over values...
foreach ($struct->key2 as $key => $value) {
    if (is_scalar($value)) {
        echo "Key $key: $value\n";
    }
}

// Let's grab a sub array to pass elsewhere
$subarray = $struct->key2->subkey1->toArray();
 

As you can see, Phly_Struct provides object oriented access to values. You can retrieve and set them just as you would object properties. Additionally, since it implements the SPL Iterator, you can loop over a Phly_Struct just like you would an array.

Now let's turn to Phly_Config:


$array = array(
    'key1 => 'value1',
    '
key2 => array(
        'subkey1' => array(
            'subsubkey1' => 'subsubvalue1',
            'subsubkey2' => 'subsubvalue2'
        ),
        'subkey2' => 'subvalue2'
    ),
    'key3' => 'value3'
);
// Load an in-memory array into the config
Phly_Config::load($array);

// Grab configuration; Phly_Config is a singleton, so this can be done at any
// time, and any configurations loaded will be available
$config = Phly_Config::getInstance();

// Load an INI file into the config, under the namespace 'ini'
Phly_Config::load('/path/to/some/config.ini', 'ini');

// Load an XML file into the config, under the namespace 'xml'
Phly_Config::load('/path/to/some/config.xml', 'xml');

// Load a PHP file returning an array into the config, under the namespace 'php'
Phly_Config::load('/path/to/some/config.php', 'php');

// By default, values are retrieved from the 'default' namespace, which is used
// if no namespace was specified when loading a config:
echo 'I am the third value: ' . $config->key3;

// Now grab the db host from the 'ini' namespace
$host = $config->ini->db->host;

// Set the db host in the xml config namespace
$config->xml->db->host = $host;

// Let's get back the whole db array from the xml config namespace
$db_data = $config->xml->db->toArray();
 

Since Phly_Config simply loads configuration structures into Phly_Structs, you get all the syntactic yumminess of that class for free. Additionally, since Phly_Config is a singleton class, you can load configurations at different points in the code, and have access to those values anywhere else.

Now, you can set values with Phly_Config. However, they will only affect the current instance; nothing gets written back to the configuration files at this point. Typically, configuration is a one way street; you want to load configuration data and access it from your program, but not change it. I may in the future add implementations for writing configurations, or at least spitting out the formatted output. In the meantime, you can always serialize the object either with PHP or as JSON.

Comments, bugfixes, and other feedback always welcome!

Posted by Matthew Weier O'Phinney in PHP at 17:00 | Comment (1) | Trackbacks (0)

Trackbacks
Trackback specific URI for this entry

No Trackbacks

Comments
Display comments as (Linear | Threaded)

Dude! I also tried to do something similar, but decided on ArrayAccess access. It is a little complex, so I haven't yet finished it, but I think your ideas are better.

I heard that parse_ini_file() doesn't work well if the ini file is malformed.
#1 Jacob Santos (Link) on 2006-05-19 22:54 (Reply)

Add Comment

Standard emoticons like :-) and ;-) are converted to images.
E-Mail addresses will not be displayed and will only be used for E-Mail notifications

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA

 
 
  • Home
  • Resume
  • Blog
  • Phly PEAR Channel
  • Contact Me
  • About this site

ZCE

Zend Education Advisory Board Member

Add to Technorati Favorites

Calendar

Back July '09
Mon Tue Wed Thu Fri Sat Sun
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31    

Quicksearch

Links

  • PHLY - PHp LibrarY
  • Paul M. Jones
  • Mike Naberezny
  • Shahar Evron
  • Planet PHP
  • Zend Where I now work
  • Garden.org Where I once worked

Archives

July 2009
June 2009
May 2009
Recent...
Older...

Categories

XML Linux
XML Personal
XML Aikido
XML Family
XML Programming
XML Dojo
XML Perl
XML PHP

All categories

Syndicate This Blog

XML RSS 0.91 feed
XML RSS 1.0 feed
XML RSS 2.0 feed
ATOM/XML ATOM 0.3 feed
ATOM/XML ATOM 1.0 feed
XML RSS 2.0 Comments

Show tagged entries

xml best practices
xml books
xml conferences
xml decorators
xml dojo
xml dpc08
xml file_fortune
xml linux
xml mvc
xml oop
xml pear
xml perl
xml personal
xml php
xml phpworks08
xml programming
xml ubuntu
xml vim
xml webinar
xml zendcon
xml zendcon08
xml zend framework
© 2004 - present, Matthew Weier O'Phinney
matthew-web <at> weierophinney.net