1. Go to this page and download the library: Download sodaho/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/ */
sodaho / container example snippets
use Sodaho\Container\Container;
$container = new Container();
// Automatically resolves dependencies via Reflection
$controller = $container->get(UserController::class);
class UserController {
public function __construct(
private UserService $userService, // Auto-resolved
private Logger $logger // Auto-resolved
) {}
}
$container = new Container();
// Factory receives the container for nested resolution
$container->set(Database::class, fn(Container $c) => new Database(
host: $_ENV['DB_HOST'],
logger: $c->get(Logger::class)
));
// Simple values
$container->set('app.name', fn() => 'My Application');
$container = new Container();
// Short syntax
$container->bind(LoggerInterface::class, FileLogger::class);
$container->bind(CacheInterface::class, RedisCache::class);
// Fluent chaining
$container = Container::create()
->bind(LoggerInterface::class, FileLogger::class)
->bind(CacheInterface::class, RedisCache::class);
// Now autowiring resolves interfaces automatically
$service = $container->get(PaymentService::class);
// PaymentService receives FileLogger for LoggerInterface parameter
$container = new Container();
$logger1 = $container->get(Logger::class);
$logger2 = $container->get(Logger::class);
$logger1 === $logger2; // true - same instance
$container = new Container([
'cacheFile' => '/var/cache/container.php',
'cacheSignature' => $_ENV['CONTAINER_CACHE_KEY'], // Required in production!
'debug' => false,
]);
// At end of bootstrap/request
$container->saveCache();
$container = new Container(); // Reads from $_ENV / getenv() automatically
// Save cache (only writes if new classes were resolved)
$container->saveCache();
// Clear cache
$container->clearCache();
// Explicit
$container = new Container(['debug' => true]);
// Or via fluent API
$container = Container::create()->setDebug(true);
// Or automatic detection via $_ENV
// APP_DEBUG=true or APP_ENV=local/dev/development
$container = new Container(); // Debug mode auto-enabled
$container = new Container();
// Log all resolved services
$container->on('resolve', function (array $data) {
error_log("Resolved: {$data['id']}");
});
// Monitor cache performance
$container->on('cacheHit', fn($data) => $metrics->increment('container.cache.hit'));
$container->on('cacheMiss', fn($data) => $metrics->increment('container.cache.miss'));
// Log errors
$container->on('error', function (array $data) {
error_log("Container error for {$data['id']}: " . $data['exception']->getMessage());
});
try {
$container->get(SomeService::class);
} catch (ContainerException $e) {
// Show to user
echo $e->getMessage(); // "Cache signature key is
use Sodaho\Container\Container;
use Sodaho\Container\Exception\ContainerException;
use Sodaho\Container\Exception\NotFoundException;
use Sodaho\Container\Exception\CacheException;
try {
$service = $container->get(SomeService::class);
} catch (NotFoundException $e) {
// Class or service not found
} catch (CacheException $e) {
// Cache read/write error or signature mismatch
} catch (ContainerException $e) {
// Any other container error (not instantiable, unresolvable parameter, etc.)
}
env
CONTAINER_CACHE_FILE=/var/cache/container.php
CONTAINER_CACHE_KEY=your-secret-key-here # Required in production!
APP_DEBUG=false
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.