Skip to content

hackphp/container

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

HackPHP Container

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