Skip to main content

Dependency Injection 101

I did have a chance to speak at Twin Cities Drupal Camp last week, the session name was "The Symfony Way".

This session was a walk through at some concepts and components behind Symfony 2, but also, I explained (at least I tried to) how to using them while working with Symfony, Silex & Drupal 8.

One of the mentioned topics was 'Dependency Injection' not sure why but seems for some reason as one of these concepts developers not related to OO are most scared of. 

In order to explain this software design pattern I did using the following image:

Power Ranger Megazor

And this example code to describe it

$redRanger = new PowerRangers\MightyMorphin\Ranger();
$redranger->setName('Jason Lee Scott');

$yellowRanger = new PowerRangers\MightyMorphin\Ranger();
$yellowRanger->setName('Trini Kwan');

$mastodonDinozord = new PowerRangers\MightyMorphin\MastodonDinozord($redRanger);
$tigerDinozord = new PowerRangers\MightyMorphin\TigerDinozord($yellowRanger);

// Initiate Megazord sequence! 
$megaZord = new PowerRangers\MightyMorphin\MegaZord();

The main goal of this talk was to show using code examples in Symfony, Silex & Drupal 8 and how the three of them are so alike because they share the same foundation libraries and then take advantage of the paradigm "Learn once, use anywhere" but what does this mean?.  It means the knowledge you acquire learning this new concepts will be easy to transport and apply on other frameworks.

You can find the slides  at this