PHP code example of gspataro / dependencyinjection

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

    

gspataro / dependencyinjection example snippets




use GSpataro\DependencyInjection\Container;

$container = new Container();

/**
 * Register a new service
 * 
 * @param string   $tag                The name that will identify this service
 * @param callable $factory            The factory that will create the instance of the class
 * @param bool     $isSingleton = true If true, the service will be instanciated once the first time
 */

$container->add("example", function (Container $c, array $params) {
    return new stdClass();
});

/**
 * Get a service
 * Note: if the service is a singleton, the $params array will be used only the first time this method is called
 * 
 * @param string $tag        The identifier of the service
 * @param array $params = [] An array of arguments used by the factory to instanciate the class
 */

$container->get("example");

/**
 * Let's assume the class of this service will need the "example" class created in the quick start guide
 * and some other informations
 */

$container->add("second_example", function (Container $c, array $params) {
    return new stdClass($c->get("example"), $params['someinfo']);
});

/**
 * Set a variable
 * 
 * @param string $key          The name of the variable
 * @param mixed  $value = null The value of the variable (if null, the method will retrieve the value instead of setting it)
 */

$container->variable("foo", "bar");

/**
 * Get a variable
 * 
 * Output: bar
 */

$container->variable("foo");

/**
 * Directly instanciate a class
 * 
 * @param callable $factory  The factory that will instanciate the class
 * @param array $params = [] Arguments needed by the factory
 */

Container::instanciate(function (Container $c, array $params) {
    return new stdClass();
});



use GSpataro\DependencyInjection\Component;
use GSpataro\DependencyInjection\Container;

/**
 * A component must extend the Component abstract class
 */

final class MyComponent extends Component
{
    /**
     * Register the services that you need
     */

    public function register(): void
    {
        $this->container->add("myservice", function (Container $c, array $params) {
            return new stdClass();
        });
    }

    /**
     * If your services need to be booted with the application, call them inside this method
     * Here you can handle everything your service needs in order to boot
     */

    public function boot(): void
    {
        $this->container->get("myservice");
    }
}

/**
 * Register components
 */

$container->loadComponents([
    MyComponent::class
]);

/**
 * Boot components
 */

$container->boot();

$container->loadComponents([
    ComponentB::class,
    ComponentA::class
]);