1. Go to this page and download the library: Download aolbrich/php-di-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/ */
aolbrich / php-di-container example snippets
composer
class ExampleClassForFunctionLevelResolve
{
/**
* @Autowire
*/
public function getResponseWithAutowiredParams(
ExampleServiceInterface $exampleService,
ExampleSubService $exampleSubService): string
{
return
$exampleService->getResponse() . ' / ' .
$exampleSubService->getResponse() . PHP_EOL;
}
}
class ExampleSetterAutowireClass
{
private ExampleServiceInterface $exampleService;
private ExampleSubService $exampleSubService;
/**
* @Autowire
*/
public function setAutowire(
ExampleServiceInterface $exampleService,
ExampleSubService $exampleSubService
) {
$this->exampleService = $exampleService;
$this->exampleSubService = $exampleSubService;
}
public function getResponse(): string
{
return
$this->exampleService->getResponse() . ' / ' .
$this->exampleSubService->getResponse() . PHP_EOL;
}
}
$container = new Container();
// Resolve as non singleton
$class = $container->get(ExampleSetterAutowireClass::class);
$class2 = $container->get(ExampleSetterAutowireClass::class);
echo $class === $class2 ? "Same class instance created\n" : "Different class instance created\n";
// Resolve as singleton
$class = $container->singleton(ExampleSetterAutowireClass::class);
$class2 = $container->singleton(ExampleSetterAutowireClass::class);
echo $class === $class2 ? "Same class instance created\n" : "Different class instance created\n";
// Autowire as Singleton
$container->set(ExampleService::class, function(Container $container) {
return $container->singleton(ExampleService::class);
});
$class = $container->get(ExampleService::class);
$class2 = $container->get(ExampleService::class);
echo $class === $class2 ? "Same class instance created\n" : "Different class instance created\n";
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.