// create container
$container = new Container();
// bind and define with class name
$container->set(Database::class);
// define with class name and bind with contract class
$container->set(CartContract::class, Cart::class);
// define with function and bind with contract class
$container->set(CommerceContract::class, function () {
return new Commerce();
});
$db = $container->get(Database::class)
$container->set('db.host', 'localhost');
$container->set('db.port', 5432);
$container->set('db.user', 'root');
$container->set('db.pass', 'pass');
$container->set(Database::class, function () {
return new Database(
$container->get('db.host'),
$container->get('db.port'),
$container->get('db.user'),
$container->get('db.pass'),
)
});
// singaton definitions
$container->singleton(Logger::class);
$a = $container->get(Logger::class);
$b = $container->get(Logger::class);
// $a === $b
$container->set(Product::class);
$a = $container->get(Product::class);
$b = $container->get(Product::class);
// $a != $b
class Database {
}
class ProductRepo {
public function construct(private Database $db) {
}
}
$container->set(Database::class);
$container->set(ProductRepo::class); // auto autowired
$container->set(Logger::class);
// auto inject during define function
$container->set(Database::class, function (Logger $logger) {
return new Database($logger);
});
$container->set(Database::class);
$findUserById = function (Database $db, int $id): User {
return new User($db->row('user', ['id' => $id]));
}
$user = $container->call($findUserById, ['id' => 3]);
class Cache {
public function construct(private Logger $logger, private string $path) {
}
}
$container->set(Logger::class);
$container->set(Cache::class, new Define(Cache::class));
$cache = $container->get(Cache::class)->resolve(['path' => __DIR__]);
class User {
public function construct(private $name = 'guest') {
}
public function getName(): string {
return $this->name;
}
}
$container->set(new Define(User::class));
$userA = $container->get(User::class)->resolve();
$userB = $container->get(User::class)->resolve(['name' => 'Mlax']);