bg_image
header

PSR-11

PSR-11 is a PHP Standard Recommendation (PHP Standard Recommendation) that defines a Container Interface for dependency injection. It establishes a standard way to interact with dependency injection containers in PHP projects.

Purpose of PSR-11

PSR-11 was introduced to ensure interoperability between different frameworks, libraries, and tools that use dependency injection containers. By adhering to this standard, developers can switch or integrate various containers without modifying their code.

Core Components of PSR-11

PSR-11 specifies two main interfaces:

  1. ContainerInterface
    This is the central interface providing methods to retrieve and check services in the container.

namespace Psr\Container;

interface ContainerInterface {
    public function get(string $id);
    public function has(string $id): bool;
}
    • get(string $id): Returns the instance (or service) registered in the container under the specified ID.
    • has(string $id): Checks whether the container has a service registered with the given ID.
  • 2. NotFoundExceptionInterface
    This is thrown when a requested service is not found in the container.

namespace Psr\Container;

interface NotFoundExceptionInterface extends ContainerExceptionInterface {
}

3. ContainerExceptionInterface
A base exception for any general errors related to the container.

Benefits of PSR-11

  • Interoperability: Enables various frameworks and libraries to use the same container.
  • Standardization: Provides a consistent API for accessing containers.
  • Extensibility: Allows developers to create their own containers that comply with PSR-11.

Typical Use Cases

PSR-11 is widely used in frameworks like Symfony, Laravel, and Zend Framework (now Laminas), which provide dependency injection containers. Libraries like PHP-DI or Pimple also support PSR-11.

Example

Here’s a basic example of using PSR-11:

use Psr\Container\ContainerInterface;

class MyService {
    public function __construct(private string $message) {}
    public function greet(): string {
        return $this->message;
    }
}

$container = new SomePSR11CompliantContainer();
$container->set('greeting_service', function() {
    return new MyService('Hello, PSR-11!');
});

if ($container->has('greeting_service')) {
    $service = $container->get('greeting_service');
    echo $service->greet(); // Output: Hello, PSR-11!
}

Conclusion

PSR-11 is an essential interface for modern PHP development, as it standardizes dependency management and resolution. It promotes flexibility and maintainability in application development.

 

 

 


Deptrac

Deptrac is a static code analysis tool for PHP applications that helps manage and enforce architectural rules in a codebase. It works by analyzing your project’s dependencies and verifying that these dependencies adhere to predefined architectural boundaries. The main goal of Deptrac is to prevent tightly coupled components and ensure a clear, maintainable structure, especially in larger or growing projects.

Key features of Deptrac:

  1. Layer Definition: It allows you to define layers in your application (e.g., controllers, services, repositories) and specify how these layers are allowed to depend on each other.
  2. Violation Detection: Deptrac detects and reports when a dependency breaks your architectural rules, helping you maintain cleaner boundaries between components.
  3. Customizable Rules: You can customize the rules and layers based on your project’s architecture, allowing for flexibility in different application designs.
  4. Integration with CI/CD: It can be integrated into CI pipelines to automatically enforce architectural rules and ensure long-term code quality.

Deptrac is especially useful in maintaining decoupling and modularity, which is crucial in scaling and refactoring projects. By catching architectural violations early, it helps avoid technical debt accumulation.

 


Dependency Injection - DI

Dependency Injection (DI) is a design pattern in software development that aims to manage and decouple dependencies between different components of a system. It is a form of Inversion of Control (IoC) where the control over the instantiation and lifecycle of objects is transferred from the application itself to an external container or framework.

Why Dependency Injection?

The main goal of Dependency Injection is to promote loose coupling and high testability in software projects. By explicitly providing a component's dependencies from the outside, the code becomes easier to test, maintain, and extend.

Advantages of Dependency Injection

  1. Loose Coupling: Components are less dependent on the exact implementation of other classes and can be easily swapped or modified.
  2. Increased Testability: Components can be tested in isolation by using mock or stub objects to simulate real dependencies.
  3. Maintainability: The code becomes more understandable and maintainable by separating responsibilities.
  4. Flexibility and Reusability: Components can be reused since they are not tightly bound to specific implementations.

Core Concepts

There are three main types of Dependency Injection:

1. Constructor Injection: Dependencies are provided through a class constructor.

public class Car {
    private Engine engine;

    // Dependency is injected via the constructor
    public Car(Engine engine) {
        this.engine = engine;
    }
}

2. Setter Injection: Dependencies are provided through setter methods.

public class Car {
    private Engine engine;

    // Dependency is injected via a setter method
    public void setEngine(Engine engine) {
        this.engine = engine;
    }
}

3. Interface Injection: Dependencies are provided through an interface that the class implements.

public interface EngineInjector {
    void injectEngine(Car car);
}

public class Car implements EngineInjector {
    private Engine engine;

    @Override
    public void injectEngine(Car car) {
        car.setEngine(new Engine());
    }
}

Example of Dependency Injection

To better illustrate the concept, let's look at a concrete example in Java.

Traditional Example Without Dependency Injection

public class Car {
    private Engine engine;

    public Car() {
        this.engine = new PetrolEngine(); // Tight coupling to PetrolEngine
    }

    public void start() {
        engine.start();
    }
}

In this case, the Car class is tightly coupled to a specific implementation (PetrolEngine). If we want to change the engine, we must modify the code in the Car class.

Example With Dependency Injection

public class Car {
    private Engine engine;

    // Constructor Injection
    public Car(Engine engine) {
        this.engine = engine;
    }

    public void start() {
        engine.start();
    }
}

public interface Engine {
    void start();
}

public class PetrolEngine implements Engine {
    @Override
    public void start() {
        System.out.println("Petrol Engine Started");
    }
}

public class ElectricEngine implements Engine {
    @Override
    public void start() {
        System.out.println("Electric Engine Started");
    }
}

Now, we can provide the Engine dependency at runtime, allowing us to switch between different engine implementations easily:

public class Main {
    public static void main(String[] args) {
        Engine petrolEngine = new PetrolEngine();
        Car carWithPetrolEngine = new Car(petrolEngine);
        carWithPetrolEngine.start();  // Output: Petrol Engine Started

        Engine electricEngine = new ElectricEngine();
        Car carWithElectricEngine = new Car(electricEngine);
        carWithElectricEngine.start();  // Output: Electric Engine Started
    }
}

Frameworks Supporting Dependency Injection

Many frameworks and libraries support and simplify Dependency Injection, such as:

  • Spring Framework: A widely-used Java framework that provides extensive support for DI.
  • Guice: A DI framework by Google for Java.
  • Dagger: Another DI framework by Google, often used in Android applications.
  • Unity: A DI container for .NET development.
  • Autofac: A popular DI framework for .NET.

Implementations in Different Programming Languages

Dependency Injection is not limited to a specific programming language and can be implemented in many languages. Here are some examples:

C# Example with Constructor Injection

public interface IEngine {
    void Start();
}

public class PetrolEngine : IEngine {
    public void Start() {
        Console.WriteLine("Petrol Engine Started");
    }
}

public class ElectricEngine : IEngine {
    public void Start() {
        Console.WriteLine("Electric Engine Started");
    }
}

public class Car {
    private IEngine _engine;

    // Constructor Injection
    public Car(IEngine engine) {
        _engine = engine;
    }

    public void Start() {
        _engine.Start();
    }
}

// Usage
IEngine petrolEngine = new PetrolEngine();
Car carWithPetrolEngine = new Car(petrolEngine);
carWithPetrolEngine.Start();  // Output: Petrol Engine Started

IEngine electricEngine = new ElectricEngine();
Car carWithElectricEngine = new Car(electricEngine);
carWithElectricEngine.Start();  // Output: Electric Engine Started

Python Example with Constructor Injection

In Python, Dependency Injection is also possible, and it's often simpler due to the dynamic nature of the language:

class Engine:
    def start(self):
        raise NotImplementedError("Start method must be implemented.")

class PetrolEngine(Engine):
    def start(self):
        print("Petrol Engine Started")

class ElectricEngine(Engine):
    def start(self):
        print("Electric Engine Started")

class Car:
    def __init__(self, engine: Engine):
        self._engine = engine

    def start(self):
        self._engine.start()

# Usage
petrol_engine = PetrolEngine()
car_with_petrol_engine = Car(petrol_engine)
car_with_petrol_engine.start()  # Output: Petrol Engine Started

electric_engine = ElectricEngine()
car_with_electric_engine = Car(electric_engine)
car_with_electric_engine.start()  # Output: Electric Engine Started

Conclusion

Dependency Injection is a powerful design pattern that helps developers create flexible, testable, and maintainable software. By decoupling components and delegating the control of dependencies to a DI framework or container, the code becomes easier to extend and understand. It is a central concept in modern software development and an essential tool for any developer.