Skip to main content

Command Palette

Search for a command to run...

Object Oriented Programming(OOP) Series: Dependency Injection (DI)

Dependency Injection (DI) in simple terms with examples

Published
3 min read
Object Oriented Programming(OOP) Series: Dependency Injection (DI)
A

I’m a Software Engineer with over 5+ years of experience in Technology with a track record in building web applications, mobile applications and Technical Writing. Readily available to explore innovations in ICT and creatively use them to build solutions. Advancing my career in software engineering and contributing to the growth of tech communities around the world. I’m also passionate about working with organizations especially tech, security experts and airline industries to learn and also make contributions that will be a legacy.

Tech is Easy to Learn - https://amazon.com/dp/B0B8T838K

In this OOP concepts series using PHP, Javascript, and Dart we've covered Classes & Objects , Attributes & Methods, Constructor & Destructor, Access modifier, Inheritance, Polymorphism, Abstraction, Encapsulation Namespaces Traits

Dependency Injection is an open compound word i.e (a combination of dependency and injection to form a new word).

Dependency: is a term used for an item that's dependent on another item. For example, my blogging dependency on Hashnode. Hashnode is a blogging service that I depend on to publish my articles without having to build and manage a blogging platform by myself.

Injection: the meaning isn't different from the connotation in the health field. It's an act of administering something into another, say for instance injecting liquid into the body using an injector.

Dependency injection: is injecting into something the dependent state for ease of access and use. What the body is dependent on to be relieved is been injected into it.

For example, the facility here on hashnode is a dependency injection that allows me to make use of hashnode blogging services to publish articles at ease as a user(client)

That been said, it means there are three things involved in this Dependency Injection example: Hashnode providing the blogging services, Blog Injector that grants me dependency access to the blog service and me depending on the service.

Dependency Injection (DI)

In the context of object-oriented programming, dependency injection is a method by which an object receives other objects it depends on by injection. The class sending the dependencies is referred to as an injector while the class receives is referred to as client. What happens in OOP as we import classes and have the need to make use of external services the codes can become messy to always write everything from scratch or import multiple statements at every instance for the same purpose. The dependency injection is a way to handle the separation of concerns and injection of services for use by the dependants.

Dependency injection addresses dependencies at runtime rather than at compile time. It can be viewed as a design pattern that's used to implement Inversion of Control (IoC).

DI in PHP

A brief example of dependency injection in PHP

//constructor injection

<?php
   class Tutuorial {

//php 8 constructor in one line with object injected
      public function __construct(private $lesson){}

//public method to access the object
      public function introduction(){
         echo 'Introduction to ' . $this->lesson[0];
      }
   }

   $lesson = ['Dependency Injection in PHP', 'Dependency', 'Injection'];
//the constructor value must be initialized before the class can be used
   $tutor = new Tutuorial($lesson);
   $tutor->introduction();
?>

constructor DI .jpg

constructor DI  output.jpg

//setter injection

<?php
   class Tutuorial {
    private $lesson;


  public function introduction($lesson){
  //set the lesson value
      $this->lesson = $lesson;
        print_r($this->lesson);
      }
   }

   $lesson = ['Dependency Injection in PHP', 'Dependency', 'Injection'];
   $tutor = new Tutuorial();
   $tutor->introduction($lesson[2]);

   ?>

setter code DI.jpg

setter output.jpg

Another way is through interface injection by using a container to manage dependencies.

Recommended for further studies on Dependency injection

https://www.freecodecamp.org/news/a-quick-intro-to-dependency-injection-what-it-is-and-when-to-use-it-7578c84fa88f/

https://en.wikipedia.org/wiki/Dependency_injection#:~:text=In%20software%20engineering%2C%20dependency%20injection,it%20depends%20on%2C%20called%20dependencies.&text=The%20'injection'%20refers%20to%20the,part%20of%20the%20client's%20state.

Thank you for reading this article.

Please kindly share with your network and feel free to use the comment section for questions, answers, and contributions.

Do you love this article?? please follow me on hashnode or Twitter @alemsbaja to stay updated for more on these OOP series?