PHP: Dependency Injection and Inversion of Control.

benitto_raj F
3 min readMar 20, 2021

Today we are going to learn DI and IOC.

What are DI and IOC?

If you say DI-Dependency Injection, IOC-Inversion of control you are right but wait “There is a difference between knowing the name of something and knowing something”. So what is exactly these two do and how it is used.

  1. Dependency Injection

Definition: “Dependency injection means one object supplies or injects the Dependencies of another object”. That’s it.

class PaymentGateWay
{
protected $gateway;

public function __construct(ChargeBee $chargeBee)
{
$this->gateway = $chargeBee;
}

public function charge()
{
$this->gateway->payNow();
}
}
class ChargeBee
{
protected $subscription;

public function __construct(ChargeBeeSubscription $subscription)
{
$this->subscription = $subscription;
}

public function removeSubscription()
{
$this->subscription->cancelSubscription();
}

public function payNow()
{
//do stuff
}
}
class ChargeBeeSubscription
{
public function createSubscription()
{
//do stuff
}

public function cancelSubscription()
{
//do stuff
}
}
$subscription = new ChargeBeeSubscription();

$chargeBee = new ChargeBee($subscription);

$gateway = new PaymentGateWay($chargeBee);

In the above code, ChargeBee class uses the ChargeBeeSubscription class,
which means if you want to create the ChargeBee object we need ChargeBeeSubscription Object. It same as PaymentGateWay and ChargeBee class. if you create an object of PaymentGateWay before that we must create a ChargeBee object.

We are injecting the ChargeBeeSubscription object to the ChargeBee constructor and the ChargeBee object to PaymentGateWay constructor.

You may think hey here ChargeBee is tightly Coupled to the PaymentGateWay. what if I want to change my payment process to Stripe. Don’t worry about this for now.That’s where IOC come into the picture.

2. Inversion of Control

Having control in multiple places or having control in a single place, which is easy to manage. Of course, if control is done in a single place we easily manage things.

Definition: “Defining once at the Framework or class level, how to implement specific features or code paths instead of multiple times in the code”. That’s it.

when we talk about Inversion of control or Design patterns the interfaces are playing a major role.

Interfaces are simply a contract. The classes which use the interface need to implement all the methods that are declared in the interface.

interface PaymentContract
{
public function removeSubscription();
public function payNow();
}

class PaymentGateWay
{
protected $gateway;

public function __construct(PaymentContract $gateway)
{
$this->gateway = $gateway;
}

public function charge()
{
$this->gateway->payNow();
}
}

class Stripe implements PaymentContract
{
protected $subscription;

public function __construct(StripeSubsciption $subscription)
{
$this->subscription = $subscription;
}

public function removeSubscription()
{
$this->subscription->cancelSubscription();
}

public function payNow()
{
//do stuff
}
}
class StripeSubscription
{
public function createSubscription()
{
//do stuff
}

public function cancelSubscription()
{
//do stuff
}
}
class ChargeBee implements PaymentContract
{
protected $subscription;

public function __construct(ChargeBeeSubscription $subscription)
{
$this->subscription = $subscription;
}

public function removeSubscription()
{
$this->subscription->cancelSubscription();
}

public function payNow()
{
//do stuff
}
}
class ChargeBeeSubscription
{
public function createSubscription()
{
//do stuff
}

public function cancelSubscription()
{
//do stuff
}
}
/** --------------------------------------------$subscription = new ChargeBeeSubscription();

$chargeBee = new ChargeBee($subscription);

$gateway = new PaymentGateWay($chargeBee);
--------------------------------------------- **///now we change the service from ChargeBee to Stripe $subscription = new StripeSubscription();$stripe = new Stripe($subscription);$gateway = new PaymentGateWay($stripe);

now look at the above code if we want to change our service ChargeBee to Stripe. The only place we need to change is where the objects are created. It could be very easy at run time. Even we can add more services that simply implement the PaymentContract interface. Everything else is the same.

That’s it for this article. Keep Learning ☺️

--

--