Jump to content

Passing a config file between classes


MrAdam

Recommended Posts

Hey up.This is more of a OO-principle question really, but basically 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 don't want to use something like the singleton or registry patterns for obvious reasons, but I would like to implement my config object in such a way that I don't have to re-instantiate it every time I need it. I'm using an autoloader so I only have to "$config = new Config();" to instantiate the object anywhere in the code base, however I build up certain properties throughout the code that I'd like available to any class, without stooping to global tactics.I guess what I would like to know is how would you implement the config side of things?Thanks,Adam

Link to comment
Share on other sites

Those are the two options, either a global variable or passing to the constructor. They both have pretty much the same limitations. If your configuration changes after creating the various classes then it will probably need to be global, unless you want to update all of the objects every time you change the configuration.

Link to comment
Share on other sites

Just a regular global variable. If configuration changes during the execution you'll need anything that is accessing the configuration to have access to the current data. A singleton could do that, or you could just use a regular global.

Link to comment
Share on other sites

Note that singletons, factories etc. are more design patterns for your particular implementation - MVC is an architectural concept that can be implemented in many ways, using factories, other OO patterns, or otherwise (even functionally).

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...