PHP code example of progphil1337 / php-di

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

    

progphil1337 / php-di example snippets


use ProgPhil1337\DependencyInjection\ClassLookup;
use ProgPhil1337\DependencyInjection\Injector;

use MyApp\YamlConfig;
use MyApp\AbstractConfig;
use MyApp\SingletonInterface;

$lookup = new ClassLookup();
$injector = new Injector($lookup);

$lookup
    // class aliases
    ->alias(YamlConfig::class, AbstractConfig::class)

    // register singletons
    ->singleton(SingletonInterface::class)

    // Register classes that cannot be created
    ->register(new YamlConfig()) 
;

// Example for alias and registering
$yamlConfig = $injector->create(AbstractConfig::class);
bash
$ composer