tags:

views:

62

answers:

1

I'm trying to come up with the best way to handle configuration in an application. At first i just used SimpleXML and put the code in a singleton.

I've decided to try to create a way to query the configuration with something like $config->getConfig('/module/name');

Do you have to use xpath to do this or is there any other method that is faster/easier to use? I know Magento uses this scheme but with a custom xpath-like syntax.

Feel free to suggest different schemes as long as it is based on XML.

SimpleXML is a nice approach however i feel the need to wrap something around it as the return values are almost always objects where I want a string value.

Could someone explain how to wrap the xpath call to return a string value if the query returns a value, or an object if the query has multiple children?

        $xml = $this->xml->xpath($path);

How do you detect if the node returned is a leaf node?

+2  A: 

I'd recommend trying SimpleXml as it allows you to write regular PHP to query the XML instead of XPath. If using XPath is a requirement, you can execute XPath expressions with it as well, but being able to write $config->module->name is quite nice, imho.

asbjornu
+1 and I totally agree with using SimpleXML.
cballou
SimpleXML is not ruled out, and SimpleXML has an xpath() method. However this is a simple method and i'm trying to figure out if there are better methods around.
Peter Lindqvist
I'm not sure how anything can be much simpler, but good luck in your search anyway. :)
asbjornu
I ended up using SimpleXML with the xpath() method wrapped in a singleton class with additional logic. All in all, it works pretty good. I can return string or SimpleXML objects, this supports the caching of configuration fragments that i often need.
Peter Lindqvist