所以,我第一次尝试设置php-di,但是我在构建器上遇到了一些麻烦。我一直得到错误:
Uncaught exception 'DINotFoundException' with message 'No entry or class found for 'IConnection'' in /path/PHPDiContainer.php'
我在哪里出错在我的容器设置?
<?php
require_once 'vendor/autoload.php';
use repositoriesConnection;
use irepositoriesIConnection;
use DIContainerBuilder;
$container = DIContainerBuilder::buildDevContainer();
$builder = new DIcontainerBuilder();
$builder->addDefinitions([
IConnection::class => DIobject(Connection::class)
]);
$container = $builder->build();
$connection = $container->get('Connection');
... Code to show it works.
?>
IConnection::class
返回完全限定类名:irepositoriesIConnection
。所以你在PHP-DI中注册了这个名称的连接。
如果你想得到它,Connection
将不匹配任何东西。你需要做的是:
$connection = $container->get('irepositoriesIConnection');
// or
$connection = $container->get(IConnection::class);