Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

flightphp/container

Repository files navigation

FlightPHP Container

Latest Stable Version Total Downloads License PHP Version Require phpstan code coverage

A lightweight Dependency Injection Container (DIC) for PHP, to manage and streamline object dependencies effectively.

Features

  • Lightweight and Efficient: Manage dependencies with ease.
  • Flexible Configuration: Easily integrate with various PHP applications.
  • PSR-11 Compliance: Ensures interoperability between containers.

Requirements

  • PHP 7.4 or higher
  • Composer installed on your system

Installation

To include the FlightPHP Container in your project, you can use Composer:

composer require flightphp/container

Simple usage

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) {
}
 */

Usage in FlightPHP Framework

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) {
}

Advance usage

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) {
 }
}
 */

License

FlightPHP Container is open-sourced software licensed under the MIT license.

AltStyle によって変換されたページ (->オリジナル) /