1. Go to this page and download the library: Download fi1a/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/ */
fi1a / dependency-injection example snippets
use Fi1a\DI\Container;
use Fi1a\DI\ContainerConfig;
use Fi1a\DI\Builder;
use Fi1a\Unit\DI\Fixtures\ClassA;
use Fi1a\Unit\DI\Fixtures\ClassAInterface;
use Fi1a\Unit\DI\Fixtures\ClassC;
use Fi1a\Unit\DI\Fixtures\ClassCInterface;
$config = new ContainerConfig();
$config->addDefinition(
Builder::build(ClassAInterface::class)
->defineClass(ClassA::class)
->getDefinition()
);
$config->addDefinition(
Builder::build(ClassCInterface::class)
->defineClass(ClassC::class)
->defineConstructor([1, true])
->getDefinition()
);
$container = new Container($config);
$container->get(ClassCInterface::class); // ClassCInterface
use Fi1a\DI\Container;
use Fi1a\DI\ContainerConfig;
use Fi1a\DI\Builder;
use Fi1a\Unit\DI\Fixtures\ClassA;
use Fi1a\Unit\DI\Fixtures\ClassAInterface;
$config = new ContainerConfig();
$config->addDefinition(
Builder::build(ClassAInterface::class)
->defineClass(ClassA::class)
->defineConstructor([
'parameter1' => 10,
])
->defineProperty('property1', 100)
->defineMethod('setProperty2', [true])
->getDefinition()
);
$container = new Container($config);
/** @var ClassA $object */
$object = $container->get(ClassAInterface::class); // ClassAInterface
$object->property1; // 100
$object->property2; // true
use Fi1a\DI\Container;
use Fi1a\DI\ContainerConfig;
use Fi1a\DI\Builder;
use Fi1a\Unit\DI\Fixtures\ClassA;
use Fi1a\Unit\DI\Fixtures\ClassAInterface;
use Fi1a\Unit\DI\Fixtures\ClassB;
$config = new ContainerConfig();
$config->addDefinition(
Builder::build(ClassAInterface::class)
->defineFactory(function (ClassB $classB) {
$instance = new ClassA($classB);
$instance->property1 = 100;
$instance->property2 = true;
return $instance;
})
->getDefinition()
);
$container = new Container($config);
/** @var ClassA $object */
$object = $container->get(ClassAInterface::class); // ClassAInterface
$object->property1; // 100
$object->property2; // true
use Fi1a\DI\Container;
use Fi1a\DI\ContainerConfig;
use Fi1a\DI\Builder;
use Fi1a\Unit\DI\Fixtures\ClassA;
use Fi1a\Unit\DI\Fixtures\ClassAInterface;
use Fi1a\Unit\DI\Fixtures\FactoryA;
$config = new ContainerConfig();
$config->addDefinition(
Builder::build(ClassAInterface::class)
->defineFactory([FactoryA::class, 'factoryStatic'])
->getDefinition()
);
$container = new Container($config);
/** @var ClassA $object */
$object = $container->get(ClassAInterface::class); // ClassAInterface
$object->property1; // 100
$object->property2; // true
use Fi1a\DI\Container;
use Fi1a\DI\ContainerConfig;
use Fi1a\DI\Builder;
use Fi1a\Unit\DI\Fixtures\ClassB;
use Fi1a\Unit\DI\Fixtures\ClassBInterface;
$config = new ContainerConfig();
$config->addDefinition(
Builder::build(ClassBInterface::class)
->defineObject(new ClassB())
->getDefinition()
);
$container = new Container($config);
/** @var ClassB $object */
$object = $container->get(ClassBInterface::class); // ClassBInterface
use Fi1a\DI\Builder;
use Fi1a\Unit\DI\Fixtures\ClassA;
use Fi1a\Unit\DI\Fixtures\ClassAInterface;
use Fi1a\Unit\DI\Fixtures\ClassC;
use Fi1a\Unit\DI\Fixtures\ClassCInterface;
di()->config()->addDefinition(
Builder::build(ClassAInterface::class)
->defineClass(ClassA::class)
->getDefinition()
);
di()->config()->addDefinition(
Builder::build(ClassCInterface::class)
->defineClass(ClassC::class)
->defineConstructor([1, true])
->getDefinition()
);
di()->get(ClassCInterface::class); // ClassCInterface
use Fi1a\DI\ArrayBuilder;
use Fi1a\DI\Container;
use Fi1a\DI\ContainerConfig;
use Fi1a\Unit\DI\Fixtures\ClassA;
use Fi1a\Unit\DI\Fixtures\ClassAInterface;
$config = new ContainerConfig();
$config->addDefinition(
$definition = ArrayBuilder::buildFromArray([
'name' => ClassAInterface::class,
'class_name' => ClassA::class,
'constructor' => [100, true],
'properties' => [
'property1' => 100,
'property2' => true,
],
'methods' => [
'setProperty1' => [100],
'setProperty2' => [true],
],
])->getDefinition()
);
$container = new Container($config);
/** @var ClassA $object */
$object = $container->get(ClassAInterface::class); // ClassAInterface
use Fi1a\DI\ContainerConfig;
use Fi1a\DI\Builder;
use Fi1a\DI\ToArray;
use Fi1a\Unit\DI\Fixtures\ClassA;
use Fi1a\Unit\DI\Fixtures\ClassAInterface;
$config = new ContainerConfig();
$definition = Builder::build(ClassAInterface::class)
->defineClass(ClassA::class)
->defineConstructor([
'parameter1' => 10,
])
->defineProperty('property1', 100)
->defineMethod('setProperty2', [true])
->getDefinition();
$config->addDefinition($definition);
$toArray = new ToArray();
$array = $toArray->collection($config->getDefinitions()); // [[...], [...]]
$arrayDefinition = $toArray->definition($definition); // [...]
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.