PHP code example of phpnomad / di-container

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

    

phpnomad / di-container example snippets




use PHPNomad\Di\Container\Container;
use YourApp\Database;
use YourApp\PostRepository;
use YourApp\PostRepositoryInterface;

$container = new Container();

// Map an interface to a concrete class.
$container->bind(PostRepository::class, PostRepositoryInterface::class);

// Bind something that needs a runtime argument via a factory.
$container->bindFactory(
    Database::class,
    fn() => new Database($dsn)
);

// Resolve. PostRepository's constructor dependencies are autowired.
$repository = $container->get(PostRepositoryInterface::class);