php - Dependency Injection - Passing Objects Around -


i'm creating based on oop principles , i'm trying apply dependency injection. i'm aware i'm possibly doing wrong here, whole di thing seems more confusing i'm still struggling quite head around it.

i've created form class output various form elements form class need @ least 2 other classes (created objects) function properly.

here's how i've got it:

$config = new config(); $db = new database(); $html = new html();  $form = new form($config, $db, $html); 

this me playing around form may not need of things i'm using them illustrate point.

now, when i'm creating form, don't want have pass 3 objects use it. solve static methods etc it's not path i'm wanting go down.

so what's best way able use 3 objects created earlier in form class?

i guess i'm wanting global type of behavior extent.

i've found couple of things talking , factories, ioc containers etc nothing explained , great. or link easy understand tutorial or i've failed find 1 myself.

i found pimple - along right lines?

well, if don't pass 3 objects in, not doing dependency injection.

to avoid parameters again, can write either wrapper class, or factory class has methods create , return form object. initialize rhe factory class $config, $db, $html once , use every form created.

so process this:

$factory= new factory($config, $db, $html); $form= $factory->createform(); 

Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -