Skip to content
CS Chetan Patil
About Projects Blog Gallery Contact
Resume
  1. Home
  2. Blog
  3. OOP in PHP

OOP in PHP 15 June 2017 · 3 min read

Dependency injection

Dependency injection is a technique where one object supplies the dependencies of another object. An injection is the passing of a dependency to a dependent object that would use it. Passing the service to the client, rather than allowing a client to…

Chetan S. Patil Software Engineer at Genpact
On this page
  1. Without Dependency Injection (DI):
  2. Then what does the Dependency Injection do us for…?
  3. After using dependency injection:

Dependency injection is a technique where one object supplies the dependencies of another object. An injection is the passing of a dependency to a dependent object that would use it. Passing the service to the client, rather than allowing a client to build or find the service, is the primitive requirement of the pattern.

This primitive requirement means that using values produced within the class from new or static methods is prohibited. The class should accept values passed in from outside.

The intent behind dependency injection is to decouple objects to the extent that no client code has to be changed simply because an object it depends on needs to be changed to a different one.

Dependency injection is one form of the broader technique of inversion of control. Rather than low level code calling up to high level code, high level code can receive lower level code that it can call down to. This inverts the typical control pattern seen in procedural programming.

Dependency injection supports the dependency inversion principle. The client delegates the responsibility of providing its dependencies to external code (the injector). The client is not allowed to call the injector code. It is the injecting code that constructs the services and calls the client to inject them. This means the client code does not need to know about the injecting code.

There are three common means for a client to accept a dependency injection: setter-, interface and constructor based injection. Setter and constructor injection differ mainly by when they can be used. Interface injection differs in that the dependency is given a chance to control its own injection. All require that separate construction code (the injector) take responsibility for introducing a client and its dependencies to each other.

For example, consider a Car object.

A Car depends on wheels, engine, fuel, battery, etc. to run. Traditionally we define the brand of such dependent objects along with the definition of the Car object.

Without Dependency Injection (DI): #

1
2
3
4
5
6
7
8
9
class Car{
 
  private Wheel wh= new IndianWheel();
 
  private Battery bt= new ExcideBattery();
 
 
  //The rest
}

Here, the Car object is responsible for creating the dependent objects.

What if we want to change the type of its dependent object – say Wheel – after the initial IndianWheel() punctures? We need to recreate the Car object with its new dependency say JapaniWheel(), but only the Car manufacturer can do that.

Then what does the Dependency Injection do us for…? #

When using dependency injection, objects are given their dependencies at run time rather than compile time (car manufacturing time). So that we can now change the Wheel whenever we want. Here, the dependency (wheel) can be injected into Car at run time.

After using dependency injection: #

Here, we are injecting the dependencies (Wheel and Battery) at runtime. Hence the term : Dependency Injection.

1
2
3
4
5
6
7
8
9
10
11
12
class Car{
  private Wheel wh= [Inject an Instance of Wheel (dependency of car) at runtime]
  private Battery bt= [Inject an Instance of Battery (dependency of car) at runtime]
  Car(Wheel wh,Battery bt) {
      this.wh = wh;
      this.bt = bt;
  }
  //Or we can have setters
  void setWheel(Wheel wh) {
      this.wh = wh;
  }
}
#OOP #PHP
Share this
Chetan S. Patil

Written by

Chetan S. Patil

Software Engineer in Pune — PHP, Symfony and SQL Server behind financial operations and payment systems.

About LinkedIn
PreviousCheck number is Fibonacci numbers or not NextLaravel Application with Admin LTE 2 implementation

Related reading

All articles

OOP in PHP · 2 min read

Constructor

What does Constructor mean? A constructor is a special method of a class or structure in OOP that initializes an object of…

20 Jan 2017

OOP in PHP · 7 min read

Dependency injection in PHP

Dependency Injection is a software design pattern that allows avoiding hard-coding dependencies and makes possible to change the dependencies both at runtime…

12 Jul 2016

OOP in PHP · 1 min read

Differences between abstract class and interface in PHP

In abstract classes this is not necessary that every method should be abstract. But in interface every method is abstract. Multiple and…

8 Jul 2016

CS Chetan Patil

Software Engineer in Pune — PHP, Symfony and SQL Server behind financial operations and payment systems.

i@chetanpatil.co.in

Site

About Projects Blog Gallery Resume Contact RSS

Elsewhere

Pune, Maharashtra · IST (UTC+5:30)

© 2026 Chetan S. Patil

Built with PHP & MySQL

Chetan Patil
About Projects Blog Gallery Contact Resume
i@chetanpatil.co.in