A lightweight Dependency Injection Container (DIC) for PHP, to manage and streamline object dependencies effectively.
- Lightweight and Efficient: Manage dependencies with ease.
- Flexible Configuration: Easily integrate with various PHP applications.
- PSR-11 Compliance: Ensures interoperability between containers.
- PHP 7.4 or higher
- Composer installed on your system
To include the FlightPHP Container in your project, you can use Composer:
composer require flightphp/container
To use the FlightPHP Container, you can create a new instance of the container and bind your dependencies:
<?php
require 'vendor/autoload.php';
use flight\Container;
$container = new Container;
$container->set(PDO::class, fn(): PDO => new PDO(
'mysql:host=localhost;dbname',
'username',
'password'
));
$pdo = $container->get(PDO::class);
var_dump($pdo);
/*
object(PDO)#3 (0) {
}
*/
You can use the FlightPHP Container in your FlightPHP application by setting the container instance:
<?php
require 'vendor/autoload.php';
use flight\Container;
$container = new Container;
$container->set(PDO::class, fn(): PDO => new PDO('sqlite::memory:'));
Flight::registerContainerHandler([$container, 'get']);
class TestController {
private PDO $pdo;
function __construct(PDO $pdo) {
$this->pdo = $pdo;
}
function index() {
var_dump($this->pdo);
}
}
Flight::route('GET /', [TestController::class, 'index']);
Flight::start();
Go and visit that route in your dev server and you'll see something like:
object(PDO)#3 (0) {
}
FlightPHP Container can resolve dependencies recursively, allowing you to bind complex objects and dependencies:
<?php
require 'vendor/autoload.php';
use flight\Container;
class User {}
interface UserRepository {
function find(int $id): ?User;
}
class PdoUserRepository implements UserRepository {
private PDO $pdo;
function __construct(PDO $pdo) {
$this->pdo = $pdo;
}
function find(int $id): ?User {
// Implementation ...
return null;
}
}
$container = new Container;
$container->set(PDO::class, static fn(): PDO => new PDO('sqlite::memory:'));
$container->set(UserRepository::class, PdoUserRepository::class);
$userRepository = $container->get(UserRepository::class);
var_dump($userRepository);
/*
object(PdoUserRepository)#4 (1) {
["pdo":"PdoUserRepository":private]=>
object(PDO)#3 (0) {
}
}
*/
FlightPHP Container is open-sourced software licensed under the MIT license.