Jump to content

How would you implement a config object?


Adam

Recommended Posts

Hey up.

 

I'm in the midst of writing my own framework based on the "MVC" architecture. I know there's already hundreds out there you could use, I'm not trying to re-invent the wheel this is more for the learning experience. I'm just curious about how others would implement the config object? I build up an array of properties throughout the code that I'd like available to every class within the code base. I know a lot of people here disagreed about the use of the singleton pattern, and globals in general, last time I mentioned it. So I'm just curious how would you do it?

 

Thanks

Adam

Link to comment
Share on other sites

First of, it all depends on how you architect your application. If you for example would use the same approach as Zend recently, a Zend_Application object to wrap the entire application then there is no need for singleton's as your otherwise global variables are class members of Zend_Application and accessible through some sort of mechanism ($this->_getParam()).

 

If Zend has taught me one thing then that's configuration-over-convention is really great and reduces hassle however the problem with Zend is that it has a serious lack of proper naming conventions among configuration options, you'll come across options like: camelCase, TitelCase, under_score. Altough ZendF2 is a great improvement due to it's backwards compatibility it will drag along ugly legacy code and I would have loved if ZendF2 would have underwent a re-write like Doctrine2

 

I've been thinking about writing my own framework using the configuration file as the starting point while adhering to configuration-over-convention. As to your original question it all depends in what ways you would like to use your configuration object. Zend for example uses it's Zend_Config object as an AbstractFactory leaving open options to extend it using various ways to load configuration files: ini, xml, csv, ..

Link to comment
Share on other sites

Thanks for the response. I was thinking of a kind of master application class, but wouldn't extending it each time create a new instance of the object, not retaining any new properties added? I understand obviously Zend do it but I'm not sure how, or just having a mental block..

 

My config class isn't at all as complexly wrapped as Zend's, currently it's just a stand alone object. It will parse ini files used for standard configuration, but the majority of the properties are added as the program flows. The properties themselves are stored within a multi-dimensional array as a class property and called/stored through ->get() and ->set() methods.

 

For example:

 

$config->get('path.href_base');

 

Would return $this->properties['path']['href_base'] - it's pretty simple but I like it.

 

I like the functionality of it in general at the moment, and the way everything is tied to the config object, but struggling to make it global (without actually making it global if you get me).

Link to comment
Share on other sites

Well it's not so much the code as the concept. I autoload objects not included already and instantiate them the usual way, no object factory or anything like that. The config object of course is different, I want to be able to call this object from any class, anywhere in the framework whilst preserving the added properties. The classes themselves are for the most part just standalone objects. There's a bootstrap type file which basically pulls everything together and instantiate the application class, passing the controller and action (that are derived from the route library) in as parameters. The application class mostly just instantiates the controller, calls the method and provides the top level exception catcher.

 

Temporarily I just took a CI style approach and created a get_config() function that returns a singleton instance of the config object. I don't like that style but, stuck for other ideas..

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.