hackphp/container

HackPHP PSR-11 compatible container

1.0 2021-08-27 05:27 UTC

This package is auto-updated.

Last update: 2024-04-27 11:31:36 UTC


README

HackPHP dependency injection container.

Installation

composer require hackphp/container

Usage

use Hackphp\Container\Container;

$container = Container::getInstance();

// bind class to the container by key = class name 
$container->bind(ApiHandler::class, fn () => new ApiHandler("123"));

// bind by key = interface.
$conainer->bind(ApiInterface::class, TestApi::class);

// bind by key = string
$container->bind("api", fn () => new Api);

// bind singleton
$container->singleton(ApiInterface::class, fn () => new ApiHandler("123"));

// bind class without instructuions
$container->bind(TestApi::class);
$container->singleton(TestApi::class);

// pass the object instead of closure or string
$apiHandler = new ApiHandler("123");
$container->instance(ApiInterface::class, $apiHandler);

// resolve binding
$container->make("api"); // use string key
$container->make(ApiInterface::class);

// PSR-11
$container->get("api");
$container->has("api") // bool