MrAdam Posted April 13, 2010 Share Posted April 13, 2010 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 More sharing options...
justsomeguy Posted April 13, 2010 Share Posted April 13, 2010 If you don't want to use globals then you need to pass the config object to the constructors of the other objects. Link to comment Share on other sites More sharing options...
MrAdam Posted April 13, 2010 Author Share Posted April 13, 2010 Thanks for the response, I thought I may have to do something like that. How would you approach it? Link to comment Share on other sites More sharing options...
justsomeguy Posted April 13, 2010 Share Posted April 13, 2010 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 More sharing options...
MrAdam Posted April 13, 2010 Author Share Posted April 13, 2010 Yeah, okay thanks for the replies. One more thing, how would you approach making it global? A singleton? Link to comment Share on other sites More sharing options...
justsomeguy Posted April 13, 2010 Share Posted April 13, 2010 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 More sharing options...
Synook Posted April 14, 2010 Share Posted April 14, 2010 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.