May 3, 201313 yr I'm learning about dependency injection in PHP to create an OOP application. I basically want to do the following: $config = new Config(); $db = new Database(); $html = new HTML(); $form = new Form($config, $db, $html); My problem is that I don't really want to pass through the 3 objects everytime I want to create a form. While I've been searching around for this I've been directed towards using a factory that will be responsible for creating the various objects. I'm planning on a factory that houses all my various classes that need dependencies, so it will have a method which will create a form, another which might create a product etc. As far as I can tell that will mean passing the factory around into other objects which again, I'm trying to avoid really if possible. So I was thinking that static methods (the only time I plan to use them) will allow me to just create the objects using factory around the whole application. Is this a good way to go about this or am I missing something totally obvious? Basically, I want to access a method from another class/object in the Form class but I don't want to have to pass this in every time. I don't really even want to pass in a factory. I guess I'm basically after a way to globally call classes and methods but without all my classes being static. Is the one static class sensible or is there something better?
May 7, 201313 yr Author I've seen something similar (the name escapes me atm) but wanted to get a straight forward explanation so I can understand it. It's looking like my best bet is downloading Pimple or similar and looking at how they do it.
Create an account or sign in to comment