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.
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.
PSR-11 specifies two main interfaces:
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.
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.
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!
}
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 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.
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) 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.
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.
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());
}
}
To better illustrate the concept, let's look at a concrete example in Java.
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.
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
}
}
Many frameworks and libraries support and simplify Dependency Injection, such as:
Dependency Injection is not limited to a specific programming language and can be implemented in many languages. Here are some examples:
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
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
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.