Note that there are some explanatory texts on larger screens.

plurals
  1. POphp dependency injection with a container
    text
    copied!<p>I'm developing a PHP framework for educational purposes. I have learned a lot since I started it.</p> <p>I've decided how I'm going to deal with dependencies. I'm create a simple DI Container.</p> <hr> <p>My <strong>first</strong> question is not about the DI Container itself, but how to inject objects that are created outside (before the DI Container).</p> <p><strong>Q:</strong> In the example: I am calling <code>container-&gt;manualAdd('_logger', $logger);</code>. Is there another way to accomplish this? Am I breaking the idea of DI Container?</p> <hr> <p>My <strong>second</strong> question is about hooking functions. So when in bootstrap all objects are instantiated, objects by it selves can now begin to function.</p> <p><strong>Q:</strong> In the example: I'm creating an EventDispatcher. Whoever needs to do something either on <code>doneBuild</code> or <code>beforeTerminate</code>, is injected with <code>BootstrapEventDispatcher</code>. Is there another way to do this?</p> <p>I begin to think <code>EventDispatcher</code> is overkill (for <code>bootstrap</code> only), and maybe implement something like: <a href="http://ellislab.com/codeigniter/user-guide/general/hooks.html" rel="nofollow">CodeIgniter:Hooks</a></p> <p>Any help is appreciated.</p> <p>Example bootstrap (pseudo-code):</p> <pre><code>function bootstrap($file = 'file.xml'){ $logger = new Logger(); $logger-&gt;log('bootstrap: init'); $dispatcher = new BootstrapEventDispatcher($logger); $container = new DIContainer(new ConfigReader($file), $logger); $container-&gt;manualAdd('_logger', $logger); $container-&gt;manualAdd('_bootstrap_event_dispatcher', $dispatcher); $container-&gt;build(); $dispatcher-&gt;doneBuild(null, new EventArgs()); $dispatcher-&gt;beforeTerminate(null, new EventArgs()); $logger-&gt;log('bootstrap: terminate'); } class DIContainer{ public function build(){ //read xmls and create classes, etc. $this-&gt;logger-&gt;log('DIContainer: creating objects: {objects}'); } } </code></pre> <p>Example of an xml:</p> <pre><code>&lt;!-- example file.xml !--&gt; &lt;services&gt; &lt;service id="simple_class" class="SimpleClass"&gt;&lt;/service&gt; &lt;service id="complex_class" class="ComplexClass"&gt; &lt;argument type="service" id="simple_class" /&gt; &lt;!-- dependency injection !--&gt; &lt;argument type="service" id="_logger" /&gt; &lt;!-- dependency injection !--&gt; &lt;argument type="service" id="_bootstrap_event_dispatcher" /&gt; &lt;!-- dependency injection !--&gt; &lt;/service&gt; &lt;/services&gt; </code></pre> <p>Example of ComplexClass:</p> <pre><code>class ComplexClass{ public function __construct(SimpleClass $simpleClass, BootstrapEventDispatcher $dispatcher, Logger $logger){ $this-&gt;simpleClass = $simpleClass; $this-&gt;logger = $logger; $dispatcher-&gt;onDoneBuild(array($this, 'onBootstrapDoneBuild')); } public function onBootstrapDoneBuild($obj, $args){ //do something. $this-&gt;logger-&gt;log('complexclass: did something'); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload