PHP code example of guide42 / suda

1. Go to this page and download the library: Download guide42/suda 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/ */

    

guide42 / suda example snippets


$di = new suda\Registry([
    MovieFinder::class => function(callable $make) {
        return new MovieFinder;
    },
    MovieLister::class => function(callable $make, MovieFinder $finder) {
        return new MovieLister($finder);
    },
]);

$finder0 = $di[MovieFinder::class];
$finder1 = $di[MovieFinder::class];

assert($finder0 === $finder1);

$di[ConcreteHouse::class] = function(callable $make) {
    $house = $make(); // creates the object resolving dependencies
    return $house;
};
$di[ConcreteHouse::class] = function(callable $make) {
    $house = $make(); // calls the previous factory
    $house->build();
    return $house;
};

$di[NumberValidatorInterface::class] = function(callable $make) {
    return $make(BCNumberValidator::class, ['min' => 5]);
};

$di = new Registry;                                    // creates empty registry
$di = new Registry(array $values);                     // ... with assoc-array containing values or factories
$di = new Registry(array $values, Registry $delegate); // ... with registry to delegate dependencies

$di[string $classOrInterface] = callable $factory;     // stores a factory for abstract
$di[string $key] = mixed $value;                       // stores a parameter

$di(callable $fn);                                     // call a function resolving it's parameters
$di(callable $fn, array $arguments);                   // ... with given arguments

$di->freeze();                                         // disallow to store values or factories
$di->freeze(string $key);                              // ... for this entry key