PHP-DI 未找到 'interface name' 的条目或 class

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

所以,我第一次尝试设置 php-di,但我在使用构建器时遇到了一些问题。我不断收到错误消息:

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

我的容器设置哪里出错了?

<?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 完全限定 class 名称:irepositories\IConnection。因此,您正在 PHP-DI.

中以该名称注册连接

想要得到,Connection什么都匹配不到。你需要做的:

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