Upgrade to Pro — share decks privately, control downloads, hide ads and more …

IoC container beyond constructor injection

IoC container beyond constructor injection

Did you know your IoC container can do a whole lot more than just constructor injection? Besides that it is actually packed with features. Inflectors, resolving callbacks, aliasing, method invocation to name a few. In this talk you will learn how to leverage the power of a great container and service providers to write better, loosely coupled code. Well designed code put together by your IoC container will make your applications SOLID, modular, lean and decoupled from the framework!

Feedback here: http://www.meetup.com/Laravel-Cologne/events/231240611/

Hannes Van De Vreken

June 28, 2016
Tweet

More Decks by Hannes Van De Vreken

Other Decks in Technology

Transcript

  1. !

  2. class SendInvoice { public function __construct(Mailer $mailer) { $this->mailer =

    $mailer; } public function listen(InvoiceGeneratedEvent $event) { … } }
  3. trait EnvironmentAware { public function setEnv($env) { $this->env = $env;

    } private function isEnv($envs) { return in_array($this->env, (array) $envs); } }
  4. CALLING THE CONTAINER Preferably only for - Factories - Event

    Listeners - Controllers - Command Handlers
  5. BONUS - SOFT DEPENDENCIES $func = function () { return

    $this->app->make('queue'); } $mailer->setQueueResolver($func);
  6. BONUS - CIRCULAR DEPENDENCIES $mailer = new Mailer(); $queue =

    new Queue(MailerInterface $mailer); $mailer->setQueueResolver(function () use ($qu return $queue; });