PHP code example of codeblanche / depend

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

    

codeblanche / depend example snippets


/*
 * Good
 */
class ClassA
{
    function __construct(ClassC $c, ClassD $d, ClassF $f) 
    {
    }
}

/*
 * Bad
 */
class ClassA
{
    function __construct($c, $d, $f) 
    {
    }
}





/*
 * Create an instance of the Manager.
 */
$dm  = new \Depend\Manager();

/*
 * Get an instance of your application class with simple 
 * dependencies automatically resolved.
 */
$app = $dm->get('MyApplication');




/*
 * Depend throws exceptions so we can catch them.
 *
 * - \Depend\Exception\InvalidArgumentException
 * - \Depend\Exception\RuntimeException
 */
try {

    /*
     * Start by creating the Manager with two optional parameters.
     *
     * (1) FactoryInterface    $factory
     * (2) DescriptorInterface $descriptorPrototype
     *
     * When the parameters are not provided Depend will use
     * it's own internal implementations.
     */
    $dm = new \Depend\Manager();

    /*
     * Create an InjectorFactory if you need to inject
     * dependencies using setters.
     *
     * For advanced developers you can override the injector
     * prototype by providing an instance of your own
     * \Depend\Abstraction\InjectorInterface implementation
     * as the first and only optional argument.
     */
    /** @var $if \Depend\InjectorFactory */
    $if = $dm->get('\Depend\InjectorFactory');

    /*
     * Load a module that implements \Depend\Abstraction\ModuleInterface
     * to defer configuration of classes and implementation to independent
     * modules.
     *
     * Our recommendation is to include a /Depend/Module.php within your module
     * root.
     *
     * Note: Module order does matter. Modules loaded later may override
     * implementation and definitions created in prior loaded modules.
     */
    $dm->module('\My\Own\Custom\Module\Depend\Module');

    /*
     * Tell the manager that any dependencies on InterfaceOne
     * should receive an instance of ClassOne.
     *
     * If your class does not implement the interface you can
     * expect an \Depend\Exception\InvalidArgumentException.
     */
    $dm->implement('InterfaceOne', 'ClassOne');

    /*
     * Some classes may need specific arguments to provided to
     * the constructor or make use of setter methods to inject
     * it's dependencies.
     *
     * To facilitate this Depend makes use of Descriptors. You
     * can use the Managers 'describe' method to describe the
     * additional      $if->create('setClassE', $dm->describe('ClassE')),
            $if->create('setClassOne', $dm->describe('ClassOne')),
            $if->create('setClassXA', $dm->describe('ClassXA')),
        )
    );
    $dm->describe('ClassE')->setActions(
        array(
            $if->create('setInterfaceOne', $dm->describe('InterfaceOne')),
        )
    );
    
    /*
     * Get an instance of the class you want. You can also provide a second
     * optional parameter array containing arguments that should override
     * any previously described arguments.
     */
    $a = $dm->get('ClassA');
}
catch (Exception $e) {
    echo $e->getMessage();
}