Skip to content

Releases: flightphp/container

v1.2.0 FlightPHP/Container - Support for singletons

22 Mar 21:12

Choose a tag to compare

Singleton example

Singleton is a creational design pattern that lets you ensure that a class has only one instance, while providing a global access point to this instance.
Read more

Now you can set classes or objects as singletons

Useful to share database connections, API clients or loggers

use flight\Container;

$container = new Container;
$container->singleton(DateTimeImmutable::class);

$dt1 = $container->get(DateTimeImmutable::class);
$dt2 = $container->get(DateTimeImmutable::class);

$dt1 === $dt2; // <-- true

You can also set objects as singletons

use flight\Container;

$container = new Container;
$initialObject = new DateTimeImmutable;
$container->singleton($initialObject);

$objectFromContainer = $container->get(DateTimeImmutable::class);

$initialObject === $objectFromContainer; // <-- true

Note

Now you can chain methods on container instance

use flight\Container;

$container = new Container;

$container
  ->set(sqlite3::class, fn() => new sqlite3(':memory:'))
  ->singleton(mysqli::class, fn() => new mysqli('localhost', 'root'))
  ->set(PDO::class, fn() => new PDO('sqlite::memory:'));

Full Changelog: v1.1.0...v1.2.0

v1.1.0 FlightPHP/Container - Support for optional constructor parameters

13 Mar 06:06

Choose a tag to compare

Constructor parameters sometimes has optional parameters, typehinted or not, but they're optional, so container could resolve that right?

Well, FlightPHP/Container 1.1.0 can :D

Example:

class MyClass {
  function __construct($param = 'value') {
    //
  }
}

$myInstance = (new Container)->get(MyClass::class);

// Now in 1.1.0 works

Full Changelog: v1.0.6...v1.1.0

v1.0.1 FlightPHP Dependency Injection Container

10 Mar 00:49

Choose a tag to compare

FlightPHP Container

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.

It resolve automatically for you:

  • Classes without constructor.
  • Classes without constructor parameters.
  • Class dependencies with default constructor parameters.

If flight/Container cannot resolve your dependency, you can set:

  • An interface to an implementation.
  • A complex object instantiation, passing a callable to
use flight\Container

$container = new Container;

$container->get(MyComplexObject::class, fn(Container $container): MyComplexObject => new MyComplexObject(
  // ... constructor parameters
));

Implement Dependency Inversion Principle in your projects easily with flight\Container

Full Changelog: v1.0.0...v1.0.1