[英]PHP-DI container configuration
這是我之前的問題(范圍很差)的后續問題,這讓我意識到我需要了解更多關於 DI 的知識:
https://github.com/PHP-DI/demo
具體來說,我看到該演示似乎正在使用默認的自動接線設置(即啟用),但仍然進行配置,如下所示
https://github.com/PHP-DI/demo/blob/master/app/config.php
use function DI\create;
use SuperBlog\Model\ArticleRepository;
use SuperBlog\Persistence\InMemoryArticleRepository;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
return [
// Bind an interface to an implementation
ArticleRepository::class => create(InMemoryArticleRepository::class),
// Configure Twig
Environment::class => function () {
$loader = new FilesystemLoader(__DIR__ . '/../src/SuperBlog/Views');
return new Environment($loader);
},
];
SQLArticleRepository
概念,如果我也有SQLArticleRepository
實現ArticleRepository
辦? 我如何能夠使用運行時值在存儲庫類型之間進行選擇?檢查此鏈接中使用容器和依賴項注入的規則部分http://php-di.org/doc/best-practices.html
你需要提供類型提示,讓 di 容器知道函數需要什么接口實現。 此外,您還必須在 di 容器中注冊/自動裝配該實現。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.