Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Dependency Injection and Factory

Featured Replies

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?

  • 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

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.