NYCPHP Meetup

[nycphp-talk] How to make a model/database accessible to different parts/classes of the application

Jakob Buchgraber jakob.buchgraber at googlemail.com
Wed Aug 29 16:31:49 EDT 2007


Hey!

I have an MVC based application and I need to have access to my Model 
object in different parts of the application (controller, templates(read 
operations), view helpers, plugins ...). So I first thought of passing 
it to each class that needs it, however it's not very elegant to pass it 
to 6 classes until it arrives at a class that actually needs it.

So my second attempt was to create a singleton, but the problem is that 
my model needs configuration data in order to be instantiated correctly. 
So whenever I would call the getInstance(...) method I need to have the 
model's configuration data available as I can't always be sure that the 
getInstance method has been called before and an instance of the model 
class already exists. It also doesn't make sense to have a configurable 
singleton object as there is always one instance, no mater whether you 
pass different configuration data to it.

Last but not least I wanted to create a Registry class, add my model to 
it and access it from whatever method I want. This however makes unit 
testing harder and has all the site effects which global variables have.

So how do you handle your model/database object? What's the best way?

Thanks!

Jakob



More information about the talk mailing list