1. Go to this page and download the library: Download iviphp/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/ */
iviphp / container example snippets
declare(strict_types=1);
use Ivi\Container\Container;
$container = new Container();
declare(strict_types=1);
use Ivi\Container\Container;
final class Database
{
}
final class UserRepository
{
public function __construct(
private Database $database
) {}
}
$container = new Container();
$repository = $container->get(UserRepository::class);
declare(strict_types=1);
use Ivi\Container\Container;
interface LoggerInterface
{
public function log(string $message): void;
}
final class FileLogger implements LoggerInterface
{
public function log(string $message): void
{
// Write the message to a file.
}
}
$container = new Container();
$container->bind(
LoggerInterface::class,
FileLogger::class
);
$logger = $container->get(LoggerInterface::class);
if ($container->has(LoggerInterface::class)) {
$logger = $container->get(LoggerInterface::class);
}
$container->forget(LoggerInterface::class);
$container->flush();
final class Service
{
public function __construct(
private UserRepository $repository
) {}
}
final class Service
{
public function __construct(
private string $environment = 'production'
) {}
}
final class Service
{
public function __construct(
private ?string $name = null
) {}
}
final class Service
{
public function __construct(
private string $name
) {}
}
final class FirstService
{
public function __construct(
SecondService $second
) {}
}
final class SecondService
{
public function __construct(
FirstService $first
) {}
}