1. Go to this page and download the library: Download selective/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/ */
selective / container example snippets
use Selective\Container\Container;
$container = new Container();
// ...
$myService = $container->get(MyService::class);
use Selective\Container\Container;
use Selective\Container\Resolver\ConstructorResolver;
$container = new Container();
// Enable autowiring
$container->addResolver(new ConstructorResolver($container));
//...
use App\Service\MyService;
use Selective\Container\Container;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
$container = new Container();
// Add definition
$container->factory(MyService::class, function (ContainerInterface $container) {
return new MyService();
});
use Psr\Container\ContainerInterface;
// ...
$entries = [
MyService::class => function (ContainerInterface $container) {
return new MyService();
},
PDO::class => function (ContainerInterface $container) {
return new PDO('sqlite:example.db');
},
// and so on...
];
$container->factories($entries);
use App\Service\MyService;
use Selective\Container\Container;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
final class MyServiceFactoryProvider
{
/**
* @return array<string, callable>
*/
public function __invoke(): array
{
return [
MyService::class => function (ContainerInterface $container) {
return new MyService($container->get(LoggerInterface::class));
},
];
}
}
$container->factories((new MyServiceFactoryProvider())());
$container->set(\App\Domain\MyService::class, new \App\Domain\MyService());
use Selective\Container\Container;
use Selective\Container\Resolver\ConstructorResolver;
use Slim\App;
use Slim\Factory\AppFactory;
itions
$container->factories(
use Monolog\Logger;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
return [
'settings' => function () {
return
return $logger;
},
// Add more definitions here...
]