PHP code example of theorx / sdic

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

    

theorx / sdic example snippets



$container = new \Theorx\SDIC\SDIC();

//Register single dependency
$container->register(DependencyClassInterface::class, function(Theorx\SDIC\SDIC $container) {

    return new DependencyClass($container->get(DependencyClassDependency::class));
});



$container->register(DependencyClassInterface::class, function(Theorx\SDIC\SDIC $container) {

    return $container->shared(DependencyClassInterface::class, function() {
        return new DependencyClass();
    });
});



$container->registerArray([
    DependencyAInterface::class => function(){
        return new DependencyA();
    },
    DependencyBInterface::class => function (){
        return new DependencyB();
    }
]);



if($container->has(DependencyAInterface::class)){
    //has dependency
}



$instance = $container->get(DependencyBInterface::class);



/**
 * Class ExampleExtension
 */
class ExampleExtension implements \Theorx\SDIC\Interfaces\SDICExtension {

    /**
     * @return array
     */
    public function registerDependencies() : array {

        return [
            DependencyDInterface::class => function() {

                return new DependencyD();
            },
            DependencyEInterface::class => function() {

                return new DependencyE();
            }
            //...
        ];
    }
}



$container->loadExtension(new ExampleExtension);



    /**
     * @author Lauri Orgla <[email protected]>
     *
     *         Register name => callback,
     *         Callback example:
     *         function ($container) {
     *              return $container->shared(NAME, function($container){
     *                  return {{shared instance}};
     *              });
     *          }
     *
     *         This method is chainable, register()->register()->register()-> etc..
     *
     * @param string   $name
     * @param callable $callback
     *
     * @return SDIC
     */
     

    /**
     * Accepts array of dependencies. Example. [name => callback]
     *
     * @author Lauri Orgla <[email protected]>
     *
     * @param array $dependencies
     */

    /**
     * Used for creating shared instances
     *
     * @author Lauri Orgla <[email protected]>
     *
     * @param string   $name
     * @param callable $callback
     *
     * @return mixed
     */

    /**
     * Load container extension which is instance of class that implements SDICExtension
     *
     * @author Lauri Orgla <[email protected]>
     *
     * @param SDICExtension $extension
     */

    /**
     * Loads a set of extensions
     *
     * @author Lauri Orgla <[email protected]>
     *
     * @param array $extensions
     */