Search code examples
phpdependency-injectionphp-di

PHP-DI No entry or class found for 'interface name'


So, I'm trying to set up php-di for the first time, but I'm having some trouble with the builder. I keep getting the error:

Uncaught exception 'DI\NotFoundException' with message 'No entry or class found for 'IConnection'' in /path/PHPDiContainer.php'

Where am I going wrong in my container setup?

<?php 
require_once 'vendor/autoload.php';

use repositories\Connection;
use irepositories\IConnection;
use DI\ContainerBuilder;

$container = DI\ContainerBuilder::buildDevContainer();
$builder = new DI\containerBuilder();
$builder->addDefinitions([
    IConnection::class => DI\object(Connection::class)
]);
$container = $builder->build();
$connection = $container->get('Connection');
... Code to show it works.
?>

Solution

  • IConnection::class returns the fully qualified class name: irepositories\IConnection. So you are registering the connection under that name in PHP-DI.

    If you want to get it, Connection will not match anything. You need to do:

    $connection = $container->get('irepositories\IConnection');
    // or
    $connection = $container->get(IConnection::class);