1. Go to this page and download the library: Download spriebsch/di 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/ */
spriebsch / di example snippets
use spriebsch\diContainer\DIContainer;
use spriebsch\diContainer\Configuration;
// Create a simple configuration
$configuration = new class implements Configuration {};
// Instantiate the container with the configuration and factory classes
$container = new DIContainer($configuration, MyFactory::class);
// Get an instance of a class
$service = $container->get(MyService::class);
class Dependency {}
class Service {
public function __construct(Dependency $dependency) {}
}
// If MyFactory does not explicitly handle Service or Dependency,
// the container will try to autowire them.
$service = $container->get(Service::class);
use spriebsch\diContainer\AbstractFactory;
final readonly class MyFactory extends AbstractFactory
{
protected function MyService(): MyService
{
return new MyService('some-parameter');
}
}
final readonly class MyFactory extends AbstractFactory
{
protected function Project_Namespace_MyService(): MyService
{
return new MyService('some-parameter');
}
}
final readonly class MyFactory extends AbstractFactory
{
protected function some_virtual_type(): MyService
{
return new MyService('virtual');
}
}
// Requesting the virtual type
$service = $container->get('some_virtual_type');
final readonly class MyFactory extends AbstractFactory
{
protected function MyService(string $param): MyService
{
return new MyService($param);
}
}
$service = $container->get(MyService::class, 'dynamic-param');
$container = new DIContainer($configuration, FactoryA::class, FactoryB::class);
final readonly class MyFactory extends AbstractFactory
{
protected function MyService(): MyService
{
// $this->configuration is available here
return new MyService($this->configuration->getSetting());
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.