1. Go to this page and download the library: Download atanvarno/dependency 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/ */
atanvarno / dependency example snippets
use Atanvarno\Dependency\Container;
use function Atanvarno\Dependency\{entry, factory, object, value};
$container = new Container();
// Add a value to the container
$container['value ID'] = 'your value';
// Add a lazy loaded class to the container
$container['class ID'] = object(YourClass::class, ['argument1', entry('value ID')]);
// Get a value from the container
$value = $container['value ID'];
var_dump($value === 'your value'); // true
// Get a class instance from the container
$instance = $container['class ID'];
var_dump($instance instanceof YourClass::class); // true
// Setting a factory
$callable = function(int $arg1, ClassName $arg2) {
// ...
};
$container->set('ID', factory($callable, [5, entry('ClassInstance')]));
// Setting an object
class ClassName
{
public function __construct(int arg1, OtherClass $arg2)
{
// ...
}
}
$container->set('ID', object(ClassName::class, [5, entry('OtherInstance')]));
// A non-registered factory
$container->get('ID', factory(function(){/*...*/}, [], false));
// A non-registered object
$container->get('ID', object(ClassName::class, [], false));
$container->set(
'ID',
object(ClassName::class, [$param1, entry('param2')])
->method('methodName', [$param3, entry('param4')]) // Call a method with parameters
->property('propertyName', 'value') // Then set a property value
->property('otherProperty', entry('aValue')) // Then set another property
);