PHP code example of flotzilla / container

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

    

flotzilla / container example snippets


 $container = new \flotzilla\Container\Container(
     [
         'EmptyTestClassDI' => EmptyTestClass::class, // class without dependencies, init by classname
         'TestClassDI' => [TestClass::class, 'message'], // class with constructor string parameter
         'TestClassDI2' => function () { // closure, that returns new class instance
             return new TestClass('test');
         },
        'ClosureDI' => function ($x, $y) { // closure, that returns sum result
             return $x + $y;
         },

         'TestClassWithDependecyDI' => [TestClassWithDependecy::class, 'TestClassDI'] // class with dependency of another service
     ]
 );

use \flotzilla\Container\Container;
$container = new Container();

$container->set('LoggerDI', function () { return new Logger();});
$container->set('ClosureDI', function ($x, $y) { return $x + $y;});
$container->set('EmptyTestClassDI', ClassWithoutConstructor::class);
$container->set('ClassDIWithDependency', [SomeClass::class, 'message']);
$container->set('AnotherClassWithDIDependency', [TestClass::class, 'LoggerDI']);

$logger = $container->get('LoggerDI');

$container->set('ClosureDI', function ($x, $y) { return $x + $y;});
$logger = $container->getWithParameters('ClosureDI', [1, 2]); // will return 3