簡體   English   中英

PHP-DI 無法解析:條目無法解析:__construct() 的參數 xxxxxx 沒有定義或可猜測的值 完整定義

[英]PHP-DI cannot be resolved: Entry cannot be resolved: Parameter xxxxxx of __construct() has no value defined or guessable Full definition

只是把它扔在這里是因為我找不到關於這個錯誤的很多信息,我花了大約 2 個小時才找到它。 臉掌

在 container.php -> DBService 中定義為:

    DBServiceInterface::class => function (ContainerInterface $c) {
        return new DBService(
            $c->get('settings.database'), 
            $c->get(SessionInterface::class), 
            $c->get(ValidatorInterface::class)
    );
    },

類型:DI\\Definition\\Exception\\InvalidDefinition 消息:無法解析條目“PVS\\HomeController”:無法解析條目“PVS\\DBService\\DBService”:__construct() 的參數 $settings 沒有定義或可猜測的值完整定義:對象( class = PVS\\DBService\\DBService lazy = false __construct( $settings = #UNDEFINED# $session = get(PVS\\Helpers\\Storage\\Contracts\\SessionInterface) $validator = get(PVS\\Validation\\Contracts\\ValidatorInterface) ) ) 完整定義: Object ( class = PVS\\HomeController lazy = false __construct( $container = get(Psr\\Container\\ContainerInterface) $view = get(Slim\\Views\\Twig) $router = get(Slim\\Router) $flash = get( Slim\\Flash\\Messages) $session = get(PVS\\Helpers\\Storage\\Contracts\\SessionInterface) $db = get(PVS\\DBService\\DBService) ) ) 文件:

所以我開始在我的容器或 DBService.php 本身中尋找問題。 問題實際上出在錯誤消息第一行的控制器中。

HomeController 的構造函數定義為:

public function __construct (ContainerInterface $container, 
    Twig $view, 
    Router $router, 
    Messages $flash, 
    SessionInterface $session, 
    DBService $db) {  <--- Problem here

我把它改成:

    public function __construct (ContainerInterface $container, 
    Twig $view, 
    Router $router, 
    Messages $flash, 
    SessionInterface $session, 
    DBServiceInterface $db) { <--- 

請注意,我現在調用的是接口而不是具體實現,它與上面發布的 DI 容器相匹配。

我有同樣的例外,簡而言之,我的問題是一個類繼承了其父類的構造函數,而父類__constructor不是公共的而是受保護的。

也許這可以節省其他人數小時的研究時間。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM