PHP Dependency Injection Based on Payment Gateway Example

Ariful Islam
3 min readMay 29, 2023

--

PHP dependency injection is a design pattern used in object-oriented programming to achieve loose coupling and improve code modularity and testability. It is a technique where the dependencies required by a class are provided from outside instead of the class itself creating or managing them.

In traditional programming, when a class needs to use another class or object, it directly creates or instantiates the required object within itself. This creates tight coupling between classes, making them dependent on each other. This can lead to several issues such as code that is hard to maintain, difficult to test in isolation, and limited flexibility in changing dependencies.

Dependency injection aims to address these problems by separating the responsibility of dependency creation and management from the dependent class. Instead of creating dependencies internally, a class’s dependencies are “injected” or provided from the outside.

There are three main types of dependency injection:

1. Constructor Injection: Dependencies are provided through a class constructor. The dependent class has a constructor that accepts the required dependencies as parameters. When creating an instance of the class, the dependencies are passed in.

2. Setter Injection: Dependencies are provided through setter methods. The dependent class has setter methods that allow the dependencies to be set after the instance is created.

3. Interface Injection: Dependencies are provided through an interface. The dependent class implements an interface that defines methods for setting dependencies. The dependencies are then set using those methods.

By using dependency injection, classes become more modular and flexible. They can easily be tested in isolation by providing mock or stub dependencies. It also promotes code reusability as the same class can be used with different dependencies. Additionally, it enables easier swapping of dependencies, making it simpler to change or upgrade components without modifying the dependent class.

Dependency injection containers or frameworks, such as Symfony’s Dependency Injection Component or Laravel’s IoC container, provide automated mechanisms for managing and resolving dependencies, reducing the manual effort required for injecting dependencies throughout an application.

Certainly! Here’s an example implementation of three payment classes (`PayPalPayment`, `StripePayment`, and `CreditCardPayment`) along with a `PaymentController` that utilizes dependency injection in PHP:

//payment interface
interface PaymentInterface {
public function processPayment();
}

Payment Methods

// PayPal payment class
class PayPalPayment implements PaymentInterface
{
public function processPayment()
{
// Logic for processing payment via PayPal
echo "Processing payment via PayPal…\n";
}
}

// Stripe payment class
class StripePayment implements PaymentInterface
{
public function processPayment()
{
// Logic for processing payment via Stripe
echo "Processing payment via Stripe…\n";
}
}

// Credit card payment class
class CreditCardPayment implements PaymentInterface
{
public function processPayment()
{
// Logic for processing payment via credit card
echo "Processing payment via credit card…\n";
}
}

Payment Controller

// Payment controller class
class PaymentController
{
private $payment;

public function __construct(PaymentInterface $payment)
{
$this->payment = $payment;
}

public function processPayment()
{
$this->payment->processPayment();
}
}

Usage

// Usage
$paypalPayment = new PayPalPayment();
$stripePayment = new StripePayment();
$creditCardPayment = new CreditCardPayment();
$paymentController = new PaymentController($paypalPayment);
$paymentController->processPayment();
$paymentController = new PaymentController($stripePayment);
$paymentController->processPayment();
$paymentController = new PaymentController($creditCardPayment);
$paymentController->processPayment();

In this example, we have an interface `PaymentInterface` that defines a `processPayment()` method. Three payment classes (`PayPalPayment`, `StripePayment`, and `CreditCardPayment`) implement this interface with their respective payment processing logic.

The `PaymentController` class takes a dependency on the `PaymentInterface` through its constructor. This means that any class implementing the `PaymentInterface` can be injected into the `PaymentController` at runtime.

By utilizing dependency injection, we can easily switch between different payment methods without modifying the `PaymentController` class. Each time we create a new instance of `PaymentController`, we pass in the desired payment class, allowing the `PaymentController` to process the payment accordingly.

In the example usage, we demonstrate creating instances of `PaymentController` with different payment methods (`$paypalPayment`, `$stripePayment`, and `$creditCardPayment`). Each time the `processPayment()` method is called on the `PaymentController`, the appropriate payment processing logic will be executed based on the injected payment class.

This approach helps to achieve loose coupling between the `PaymentController` and the payment classes, making the code more flexible, maintainable, and testable.

For more clearance and better idea you may watch this video tutorial

--

--

Ariful Islam

Software Engineer. Working on PHP, Laravel, CI, CakePhp, API. Open Source contributor, Cloud Expert, Project Consultant; https://linkedin.com/in/arif98741