PHP code example of hackphp / container
1. Go to this page and download the library: Download hackphp/container library. Choose the download type require. 2. Extract the ZIP file and open the index.php. 3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
hackphp / container example snippets
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