Object Oriented Programming(OOP) Series: Dependency Injection (DI)
Dependency Injection (DI) in simple terms with examples
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();
?>
//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]);
?>
Another way is through interface injection by using a container to manage dependencies.
Recommended for further studies on Dependency injection
freecodecamp.org/news/a-quick-intro-to-depe..
en.wikipedia.org/wiki/Dependency_injection#...
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?