简体   繁体   中英

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.
?>

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM