PHP code example of gonzalo123 / injector

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

    

gonzalo123 / injector example snippets


namespace Foo

class Math
{
    public function sum($i, $j)
    {
        return $i+$j;
    }
}



use Silex\Application;
use Foo\Math;

$app            = new Application(['debug' => true]);

$app['math'] = function () {
    return new Math();
};

$app->get("/", function () use ($app) {
    return $app['math']->sum(1, 2);
});

$app->run();



use Silex\Application;
use Injector\InjectorServiceProvider;
use Foo\Math;

$app            = new Application(['debug' => true]);

$app->register(new InjectorServiceProvider([
    'Foo\Math' => 'math',
]));

$app['math'] = function () {
    return new Math();
};

$app->get("/", function (Math $math) {
    return $math->sum(1, 2);
});

$app->run();