Time for the final modifications to my dependency injection container implementation which I’ve shown in my last two blog posts. This time I am making some parts configurable, adding the possibility to configure injected objects and… well, see yourself.
Here we go again. Building a DI-Container with Reflection, making use of autowiring, for constructor and setter injection.
Some headaches later I think I found a nice way to implement a Dependency Injection Container in PHP. I’ve been confused by some containers which I believed are using the Service Locator pattern instead of dependency injection to fulfill Inversion of Control.
And finally got my Interface Injection working… You’ve got no idea what I am writing about?
Some blogs earlier I had an example for the singleton pattern. Here it is again (revised).
The state pattern becomes useful if your object needs to react differently to different states. Let us assume you are writing an SMTP Proxy. SMTP works in sequences (connection establishment, ehlo/helo, mail, rcpt, data(, …), quit). Each sequence can have different error/success codes which are not available in another. For example you would only return 220 on connection establishment, not 250. And you would return 221 on quit, not 250. So if you are writing a server which works like this, you will most likely use an if then else or switch. The state-pattern would be another method to solve this. However, a more simple example which shows the pros and cons of the state pattern is an activity-based-sleep.
Wondering why the link disappears if you do click a dropdown-toggle?
in functions.php comment out:
wp_localize_script( 'twentyfifteen-script', 'screenReaderText', array(
'expand' => '' . __( 'expand child menu', 'twentyfifteen' ) . '',
'collapse' => '' . __( 'collapse child menu', 'twentyfifteen' ) . '',
and the following in functions.js
$( '.main-navigation .menu-item-has-children > a' ).after( '<button class="dropdown-toggle" aria-expanded="false">' + screenReaderText.expand + '</button>' );
and because that leads to an error about screnReaderText not being defined, comment out the following while you are at it in functions.js:
_this.html( _this.html() === screenReaderText.expand ? screenReaderText.collapse : screenReaderText.expand );