1. Go to this page and download the library: Download corpus/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/ */
corpus / di example snippets
new \Corpus\Di\Di;
// Eager Loading
$di->set('foo', new Foo);
$di->get('foo'); // the Foo instance from above
// --- --- --- --- --- ---
// Lazy Loading
$di->set('bar', function () {
return new Bar;
});
// Value is memoized, new Bar() is only called once at first `get`.
$bar1 = $di->get('bar');
$bar2 = $di->get('bar');
// --- --- --- --- --- ---
// Constructor Parameters
$di->set('baz', function ( $qux ) {
return new Baz($qux);
});
// Calling getNew explicitly avoids the memoization. Constructor params passed as array.
$baz = $di->getNew('baz', [ 'corge' ]);
$baz2 = $di->getNew('baz', [ 'grault' ]);
// --- --- --- --- --- ---
// Auto-Constructor Parametrization
$di->set('qux', Qux::class);
$qux1 = $di->get('qux'); // New instance of Qux
$qux2 = $di->get('qux'); // Memoized instance of Qux
// --- --- --- --- --- ---
// Lazy Loading with auto-arguments.
$di->set('quux', function ( Qux $qux ) {
return new Quux($qux);
});
$quux = $di->get('quux'); // Instance of Quux given the previous instance of Qux automatically
// --- --- --- --- --- ---
// getMany lets you retrieve multiple memoized values at once.
[$foo, $bar] = $di->getMany([ 'foo', 'bar' ]);
// getManyNew lets you retrieve multiple new values at once, providing for arguments.
[$baz, $baz2] = $di->getManyNew([ [ 'baz', [ 'corge' ] ], [ 'baz', [ 'grault' ] ] ]);
$di->callFromReflectiveParams(function (Bar $bar, Baz $baz){
// Callable called with parameters automatically populated based on their name
// $bar => 'bar'
});
// Construct a class auto-populating constructor parameters based on their name
$controller1 = $di->constructFromReflectiveParams(MyController::class);
$controller2 = $di->constructFromReflectiveParams('MyController');
function getMany(array $ids) : array
function get($id)
function getManyNew(array $data) : array
function getNew(string $id [, array $args = []])
function duplicate(string $src, string $dest)
function set(string $id, $value)
function has($id) : bool
function raw(string $id)
function constructFromReflectiveParams(string $className [, array $initials = []]) : object
function callFromReflectiveParams(callable $callable [, array $initials = []])
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.