PHP code example of ob-ivan / sd-dependency-injection
1. Go to this page and download the library: Download ob-ivan/sd-dependency-injection 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/ */
ob-ivan / sd-dependency-injection example snippets
$container = new SD\DependencyInjection\Container([
'name' => 'Chewbaka', // not interpreted as a class name
]);
// Setter injection - use when constructing a Service is cheap and doesn't involve resource allocating.
$container->register('helloWorld', new HelloWorldService('Anakin'));
// Constructor injection + Setter injection - use when Service does not ld', function ($name) {
if ($name === strtoupper($name)) {
return new HelloShouterService($name);
}
return new HelloWorldService($name);
});
// No injection - use for setting parameters.
$container->register('name', $container->value('Skywalker'));
// The 'container' common name is reserved to refer to the container itself.
$container === $container->get('container');
// Initial registering:
$container->register('currency', SD\Currency\Repository::class);
// Later on:
$container->extend('currency', function ($container, $currency) {
$store = $container->produce(SD\CurrencyStore\Wpdb::class);
$currency->setStore($store);
return $currency;
});
// Inject into an already instantiated consumer:
$serviceAwareCalculator = $container->inject($serviceUnawareCalculator);
// Inject into a callable:
$response = $container->inject(function ($helloWorldService) use ($name) {
return $helloWorldService->greet($name);
});
$legacyConsumer = new LegacyConsumer($container->get('brand_new_service'));
trait ExampleAwareTrait
{
// Here's the magic. Create a field named starting with $autoDeclare and containing the service's
// common name as a value. Don't forget to import AutoDeclarerTrait!
protected $autoDeclareExample = 'example';
private $example;
// Setter name must match the common name.
public function setExample(ExampleService $example) {
$this->example = $example;
}
// An example way to access an example service instance.
protected function getExample()
{
return $this->example;
}
}
use SD\DependencyInjection\AutoDeclarerInterface;
use SD\DependencyInjection\AutoDeclarerTrait;
class ExampleConsumer implements AutoDeclarerInterface
{
use AutoDeclarerTrait;
use ExampleAwareTrait;
public function run()
{
return $this->getExample()->doSomething();
}
}
use SD\DependencyInjection\ProviderInterface;
// This provides a correspondence between the service instance and its common name.
class ExampleProvider implements ProviderInterface
{
public function getServiceName(): string
{
return 'example'; // the common name
}
public function provide()
{
return new ExampleService();
}
}
$container->connect(new ExampleProvider());
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.