PHP code example of hafo / di

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

    

hafo / di example snippets


use Hafo\DI\ContainerBuilder;

$builder = new ContainerBuilder();

// configure the builder
$builder->addFactories([
    Symfony\Component\Console\Application::class => function () {
        return new Symfony\Component\Console\Application();
    }
]);

$container = $builder->createContainer();

// run the application
$application = $container->get(Symfony\Component\Console\Application::class);
$application->run();

$builder->addFactories([
    Symfony\Component\Console\Application::class => function () {
        return new Symfony\Component\Console\Application();
    },
    // ... add more services
]);

use Hafo\DI\Container;

$builder->addDecorators([
    Symfony\Component\Console\Application::class => [
        function (Symfony\Component\Console\Application $application, Container $container) {
            $application->add($container->get(MyProject\Console\SomeCommand::class));
        },
        // ... add more decorators for Application class
    ],
    // ... add more decorators for other services
]);

$builder->addParameters([
    'rootDir' => __DIR__,
    // ... add more parameters
]);

use Hafo\DI\Autowiring\AutowiringCache\MemoryCache;
use Hafo\DI\Autowiring\DefaultAutowiring;
use Hafo\DI\ContainerBuilder;

$builder = new ContainerBuilder();

$builder->setAutowiring(new DefaultAutowiring(new MemoryCache()));

$container = $builder->createContainer();

$builder->addInterfaceImplementationMap([
    Doctrine\ORM\EntityManagerInterface::class => Doctrine\ORM\EntityManager::class
]);