GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - flightphp/container: FlightPHP Dependency Injection Container

BranchesTags
Open more actions menu

Latest commit

 

History

15 Commits
15 Commits

Folders and files

NameName
Last commit message
Last commit date
src
src
 
 
tests
tests
 
 
.editorconfig
.editorconfig
 
 
.gitattributes
.gitattributes
 
 
.gitignore
.gitignore
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
composer.json
composer.json
 
 
composer.lock
composer.lock
 
 
phpstan.dist.neon
phpstan.dist.neon
 
 
phpunit.xml.dist
phpunit.xml.dist
 
 
rector.php
rector.php
 
 

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.

About

FlightPHP Dependency Injection Container

docs.flightphp.com/learn/dependency-injection-container

Topics

containerdependency-injection-containerflightphpphp

Resources

Readme
MIT license
Activity
Custom properties

Stars

8 stars

Watchers

2 watching

Forks

0 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.