PHP code example of gixx / worstpractice-dependency-injection

1. Go to this page and download the library: Download gixx/worstpractice-dependency-injection 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/ */

    

gixx / worstpractice-dependency-injection example snippets


namespace MyNamespace;

use WorstPractice\Component\DependencyInjection\ConfigParser\ArrayParser;
use WorstPractice\Component\DependencyInjection\ServiceLibrary;
use WorstPractice\Component\DependencyInjection\Container;

$config = [
    'ServiceAlias' => [
        'class' => \Namespace\To\MyClass::class,
        'arguments' => [
            \Namespace\To\OtherClassInterface::class,
            'literalParameter' => 1234,
            'otherLiteralParameter' => false
        ],
        'shared' => false           
    ],
    \Namespace\To\OtherClassInterface::class => [
        'class' => \Namespace\To\OtherClass::class,
        'shared' => true
    ],
    \DateTimeZone::class => [
        'arguments' => [
            'param' => 'Europe/Berlin'
        ],
        'shared' => true
    ],
    \DateTime::class => [
        'calls' => [
            ['setTimezone', [\DateTimeZone::class]]
        ],
        'shared' => true
    ],
    'Auth' => [
        // empty, will be determined later
    ],
    'OtherServiceAlias' => [
        'inherits' => 'ServiceAlias',
        'calls' => [
            ['someMethod', ['parameter1' => 4543, 'parameter2' => [0, 1, 2], \DateTime::class]]
        ],
        'shared' => true       
    ],
    'LoginController' => [
        'class' => \Namespace\To\Controller\Login:class,
        'arguments' => [
            'Auth',
            'OtherServiceAlias'
        ]   
    ]
];

$container = new Container(new ServiceLibrary(new ArrayParser()), $config);

$authService = $_ENV['environment'] === 'dev'
    ? new \Namespace\To\DebugAuthService()
    : new \Namepace\To\Strict\AuthenticationService();
$isShared = true;

$container->set('Auth', $authService, $isShared);

$controller = $container->get('LoginController');

docker-compose up -d
docker exec -it worstpractice-dependency-injection-php-fpm php composer.phar install
docker exec -it worstpractice-dependency-injection-php-fpm php composer.phar check