SlideShare a Scribd company logo
Dependency injection in Drupal 8 : DrupalCon NOLA
Dependency injection in
Drupal 8
Ashwini Kumar
Who am I?
Ashwini Kumar
Tech Architect
@ashclimb
Agenda
✤ Mystery about DI
✤ Why to give my 2 cents to learn DI?
✤ DI in Symfony
✤ DI in Drupal 8
What is Dependency Injection?
Well, the literal meaning is injecting the dependency into
your Class.
DI Ground zero
DI Ground zero
DI Ground zero
DI Ground zero
Types of Dependency Injection
✤ Constructor Injection
✤ Setter Injection
✤ Property Injection
Constructor Injection
class NewsletterManager {
protected $mailer;
public function __construct(Mailer $mailer) {
$this->mailer = $mailer;
}
// ...
}
Setter Injection
class NewsletterManager {
protected $mailer;
public function setMailer(Mailer $mailer) {
$this->mailer = $mailer;
}
// ...
}
Property Injection
class NewsletterManager {
public $mailer;
// ...
}
Why Dependency Injection?
Dependency injection in Drupal 8 : DrupalCon NOLA
Dependency injection in Drupal 8 : DrupalCon NOLA
Dependency injection in Drupal 8 : DrupalCon NOLA
Without dependency injection
class GoogleMaps {
public function getCoordinatesFromAddress($address) {
// calls Google Maps webservice
}
}
class OpenStreetMap {
public function getCoordinatesFromAddress($address) {
// calls OpenStreetMap webservice
}
}
class StoreService {
public function getStoreCoordinates($store) {
$geolocationService = new GoogleMaps();
// or $geolocationService = GoogleMaps::getInstance() if you use singletons
return $geolocationService->getCoordinatesFromAddress($store->getAddress());
}
}
Without dependency injection
With dependency injectionclass StoreService {
private $geolocationService;
public function __construct(GeolocationService $geolocationService) {
$this->geolocationService = $geolocationService;
}
public function getStoreCoordinates($store) {
return $this->geolocationService->getCoordinatesFromAddress($store-
>getAddress());
}
}
With dependency injection
interface GeolocationService {
public function getCoordinatesFromAddress($address);
}
class GoogleMaps implements GeolocationService { ...
class OpenStreetMap implements GeolocationService { ...
What we also get from this…
✤ Cleaner and Modular
✤ Reusable and Flexible
✤ Abstraction
✤ Testable
DI in a Framework
Dependency Injector
==
Dependency Injection Container(DIC)
==
IoC Container
==
Service Container
er
What is Service Container?
It basically deals with creating a map of your dependencies
which your class needs and check do we have it loaded?
Symfony’s Dependency injection
component
Symfony’s DI Component
✤ Services Keys are strings, e.g.
‘some_service’
✤ Service definitions in addition to specifying
which class to instantiate and what
constructor arguments to pass in, allow you
to specify additional methods to call on the
object after instantiation.
Symfony’s DI Component
✤ Default scope: Container
✤ Can be configured in PHP, XML or YAML
✤ Can be “compiled” down to plain PHP
Dependency Injection in Drupal 8
D8 Services mostly we will be using
✤ The default DB connection (‘database’)
✤ The module handler (‘module handler’)
✤ The HTTP request object (‘request’)
2 ways you can use core’s
services
1. Service location : From procedural code,
using a helper:
Drupal::service(‘some_service’)
2. DI way : Write OO code and get wired
into the container
How and Where does it all happen?
✤ The Drupal Kernel: code/lib/Drupal/Core/DrupalKernel.php
✤ Services are defined in : core/core.services.yml
✤ Compiler passes get added in: core/lib/Drupal/Core/CoreBundle.php
✤ Compiler pass classes live here:
core/lib/Drupal/Core/DependenciesInjection/compiler/…
How does it happen for my Module?
✤ Services are defined in : mymodule/mymodule.services.yml
✤ Compiler passes get added in: my
module/lib/Drupal/mymodule/MymoduleBundle.php
✤ All classes, including compiler pass classes must live in : my
module/lib/Drupal/mymodule/
What Magic do we have in *.services.yml ?
services:
# Defines a simple service of which requires no parameter for its constructor.
example.simple:
class: DrupalExampleSimple
# Defines a service which requires the module_handler for its constructor.
example.with_module_handler
class: DrupalExampleWithModuleHandler
arguments: ['@module_handler']
Implement:Dependency Injection for a Form
https://api.drupal.org/api/drupal/services
“Start Small, then work your way up!”
~ The person who is quoted
Questions?
So How Was It? - Tell Us What You
Think
Evaluate this session - thisistheurltotheschedule
Thanks!

More Related Content

PDF
The quest for global design principles (SymfonyLive Berlin 2015)
PDF
Dependency injection in Drupal 8
PDF
Dependency Injection and Pimple
PDF
Dependency Injection in Drupal 8
PDF
Symfony & Javascript. Combining the best of two worlds
PDF
Guard Authentication: Powerful, Beautiful Security
PDF
Symfony Messenger (Symfony Live San Francisco)
PDF
Symfony tips and tricks
The quest for global design principles (SymfonyLive Berlin 2015)
Dependency injection in Drupal 8
Dependency Injection and Pimple
Dependency Injection in Drupal 8
Symfony & Javascript. Combining the best of two worlds
Guard Authentication: Powerful, Beautiful Security
Symfony Messenger (Symfony Live San Francisco)
Symfony tips and tricks

What's hot (20)

PPTX
Domain Driven Design using Laravel
PDF
New Symfony Tips & Tricks (SymfonyCon Paris 2015)
PPTX
Java script basic
PPTX
Only oop
PDF
Design how your objects talk through mocking
PDF
Love and Loss: A Symfony Security Play
PDF
Angular Directives from Scratch
PDF
Modern JavaScript Programming
PDF
How I started to love design patterns
PDF
Design Patterns in PHP5
ODP
Angularjs
DOCX
Dot Net Accenture
PPTX
Javascript for the c# developer
PDF
Angular custom directives
PDF
A Gentle Introduction To Object Oriented Php
PDF
Advanced Interfaces and Repositories in Laravel
PPTX
Object oriented php
ODP
Rich domain model with symfony 2.5 and doctrine 2.5
PPTX
JavaScript Literacy
PPT
Oops concepts in php
Domain Driven Design using Laravel
New Symfony Tips & Tricks (SymfonyCon Paris 2015)
Java script basic
Only oop
Design how your objects talk through mocking
Love and Loss: A Symfony Security Play
Angular Directives from Scratch
Modern JavaScript Programming
How I started to love design patterns
Design Patterns in PHP5
Angularjs
Dot Net Accenture
Javascript for the c# developer
Angular custom directives
A Gentle Introduction To Object Oriented Php
Advanced Interfaces and Repositories in Laravel
Object oriented php
Rich domain model with symfony 2.5 and doctrine 2.5
JavaScript Literacy
Oops concepts in php
Ad

Similar to Dependency injection in Drupal 8 : DrupalCon NOLA (20)

PPTX
Dependency Injection in Drupal 8
PDF
Dependency injection Drupal Camp Wrocław 2014
PDF
DIC To The Limit – deSymfonyDay, Barcelona 2014
PDF
Drupal 8 Vocab Lesson
ODP
Dependency Injection, Zend Framework and Symfony Container
PPT
Future Proofing Your Drupal Skills
PDF
Final dependency presentation.odp
PPTX
Dependency injection with Symfony 2
PPTX
Services, dependency injection and containers
PDF
Drupal 8 Services And Dependency Injection
PPTX
Drupal 8 Vocabulary Lesson
ODP
Dependency injection explained (Zbigniew Lukasiak)
ODP
Learning Symfony2 by practice
PDF
Drupal 7 migrating to drupal 8
PDF
Leveraging the Command Pattern: Enhancing Drupal with Symfony Messenger.pdf
PDF
Dependency Injection in PHP
PDF
Symfony 4.4 Dependency Injection Improvements
PDF
14 Dependency Injection #burningkeyboards
PDF
Drupal 8 Services
PDF
Symfony2 from the Trenches
Dependency Injection in Drupal 8
Dependency injection Drupal Camp Wrocław 2014
DIC To The Limit – deSymfonyDay, Barcelona 2014
Drupal 8 Vocab Lesson
Dependency Injection, Zend Framework and Symfony Container
Future Proofing Your Drupal Skills
Final dependency presentation.odp
Dependency injection with Symfony 2
Services, dependency injection and containers
Drupal 8 Services And Dependency Injection
Drupal 8 Vocabulary Lesson
Dependency injection explained (Zbigniew Lukasiak)
Learning Symfony2 by practice
Drupal 7 migrating to drupal 8
Leveraging the Command Pattern: Enhancing Drupal with Symfony Messenger.pdf
Dependency Injection in PHP
Symfony 4.4 Dependency Injection Improvements
14 Dependency Injection #burningkeyboards
Drupal 8 Services
Symfony2 from the Trenches
Ad

Recently uploaded (20)

PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPT
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
OOP with Java - Java Introduction (Basics)
PPT
Project quality management in manufacturing
PDF
Digital Logic Computer Design lecture notes
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PDF
PPT on Performance Review to get promotions
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PDF
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
Construction Project Organization Group 2.pptx
PPTX
Safety Seminar civil to be ensured for safe working.
PPT
introduction to datamining and warehousing
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
Sustainable Sites - Green Building Construction
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
Model Code of Practice - Construction Work - 21102022 .pdf
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
OOP with Java - Java Introduction (Basics)
Project quality management in manufacturing
Digital Logic Computer Design lecture notes
Foundation to blockchain - A guide to Blockchain Tech
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPT on Performance Review to get promotions
R24 SURVEYING LAB MANUAL for civil enggi
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Construction Project Organization Group 2.pptx
Safety Seminar civil to be ensured for safe working.
introduction to datamining and warehousing
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Sustainable Sites - Green Building Construction
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx

Dependency injection in Drupal 8 : DrupalCon NOLA

  • 3. Who am I? Ashwini Kumar Tech Architect @ashclimb
  • 4. Agenda ✤ Mystery about DI ✤ Why to give my 2 cents to learn DI? ✤ DI in Symfony ✤ DI in Drupal 8
  • 5. What is Dependency Injection? Well, the literal meaning is injecting the dependency into your Class.
  • 10. Types of Dependency Injection ✤ Constructor Injection ✤ Setter Injection ✤ Property Injection
  • 11. Constructor Injection class NewsletterManager { protected $mailer; public function __construct(Mailer $mailer) { $this->mailer = $mailer; } // ... }
  • 12. Setter Injection class NewsletterManager { protected $mailer; public function setMailer(Mailer $mailer) { $this->mailer = $mailer; } // ... }
  • 13. Property Injection class NewsletterManager { public $mailer; // ... }
  • 18. Without dependency injection class GoogleMaps { public function getCoordinatesFromAddress($address) { // calls Google Maps webservice } } class OpenStreetMap { public function getCoordinatesFromAddress($address) { // calls OpenStreetMap webservice } }
  • 19. class StoreService { public function getStoreCoordinates($store) { $geolocationService = new GoogleMaps(); // or $geolocationService = GoogleMaps::getInstance() if you use singletons return $geolocationService->getCoordinatesFromAddress($store->getAddress()); } } Without dependency injection
  • 20. With dependency injectionclass StoreService { private $geolocationService; public function __construct(GeolocationService $geolocationService) { $this->geolocationService = $geolocationService; } public function getStoreCoordinates($store) { return $this->geolocationService->getCoordinatesFromAddress($store- >getAddress()); } }
  • 21. With dependency injection interface GeolocationService { public function getCoordinatesFromAddress($address); } class GoogleMaps implements GeolocationService { ... class OpenStreetMap implements GeolocationService { ...
  • 22. What we also get from this… ✤ Cleaner and Modular ✤ Reusable and Flexible ✤ Abstraction ✤ Testable
  • 23. DI in a Framework Dependency Injector == Dependency Injection Container(DIC) == IoC Container == Service Container er
  • 24. What is Service Container? It basically deals with creating a map of your dependencies which your class needs and check do we have it loaded?
  • 26. Symfony’s DI Component ✤ Services Keys are strings, e.g. ‘some_service’ ✤ Service definitions in addition to specifying which class to instantiate and what constructor arguments to pass in, allow you to specify additional methods to call on the object after instantiation.
  • 27. Symfony’s DI Component ✤ Default scope: Container ✤ Can be configured in PHP, XML or YAML ✤ Can be “compiled” down to plain PHP
  • 29. D8 Services mostly we will be using ✤ The default DB connection (‘database’) ✤ The module handler (‘module handler’) ✤ The HTTP request object (‘request’)
  • 30. 2 ways you can use core’s services 1. Service location : From procedural code, using a helper: Drupal::service(‘some_service’) 2. DI way : Write OO code and get wired into the container
  • 31. How and Where does it all happen? ✤ The Drupal Kernel: code/lib/Drupal/Core/DrupalKernel.php ✤ Services are defined in : core/core.services.yml ✤ Compiler passes get added in: core/lib/Drupal/Core/CoreBundle.php ✤ Compiler pass classes live here: core/lib/Drupal/Core/DependenciesInjection/compiler/…
  • 32. How does it happen for my Module? ✤ Services are defined in : mymodule/mymodule.services.yml ✤ Compiler passes get added in: my module/lib/Drupal/mymodule/MymoduleBundle.php ✤ All classes, including compiler pass classes must live in : my module/lib/Drupal/mymodule/
  • 33. What Magic do we have in *.services.yml ? services: # Defines a simple service of which requires no parameter for its constructor. example.simple: class: DrupalExampleSimple # Defines a service which requires the module_handler for its constructor. example.with_module_handler class: DrupalExampleWithModuleHandler arguments: ['@module_handler']
  • 36. “Start Small, then work your way up!” ~ The person who is quoted
  • 38. So How Was It? - Tell Us What You Think Evaluate this session - thisistheurltotheschedule Thanks!