1. Go to this page and download the library: Download ody/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/ */
ody / container example snippets
use Ody\Container\Container;
$container = new Container();
// Bind an interface to a concrete implementation
$container->bind('App\Contracts\UserRepository', 'App\Repositories\DatabaseUserRepository');
// Bind with a closure
$container->bind('database', function ($container) {
return new PDO('mysql:host=localhost;dbname=myapp', 'username', 'password');
});
$container->singleton('App\Services\PaymentGateway', function ($container) {
return new App\Services\StripePaymentGateway($container->make('config'));
});
$config = new Config(['api_key' => 'your-api-key']);
$container->instance('config', $config);
// Resolve from the container
$userRepository = $container->make('App\Contracts\UserRepository');
// Using array access notation
$database = $container['database'];
class UserController
{
protected $repository;
public function __construct(UserRepository $repository)
{
$this->repository = $repository;
}
}
// The container will automatically resolve the UserRepository dependency
$controller = $container->make(UserController::class);
$container->bind('SpeedReport', function () {
return new SpeedReport();
});
$container->bind('MemoryReport', function () {
return new MemoryReport();
});
$container->tag(['SpeedReport', 'MemoryReport'], 'reports');
// Resolve all services with the 'reports' tag
$reports = $container->tagged('reports');
$container->bind('logger', function () {
return new FileLogger();
});
$container->extend('logger', function ($logger, $container) {
return new LogDecorator($logger);
});
// Register a scoped binding
$container->scoped('database.connection', function ($container) {
return new DbConnection($container->make('config'));
});
// Clear scoped instances between requests
$container->forgetScopedInstances();
use Ody\Container\Container;
use Ody\Container\ContainerHelper;
$container = new Container();
$config = iner($container, $config);
// Register all controllers in a directory
ContainerHelper::registerControllers($container, __DIR__ . '/app/Controllers');
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.