簡體   English   中英

在PHP-DI中,has()函數不適用於文件中的定義

[英]In PHP-DI the has() function not working for definition in file

使用php定義文件我創建了這個定義

return [
'auth' => \DI\object('MyProject\Users\Handlers\Permissions')->lazy()
];

但是當我使用has()函數來檢查定義是否存在時

$container->has('auth'); //this returns FALSE

但是get()函數設法返回對象。

$container->get('auth') //returns the referenced object

編輯:應用程序有點復雜,所以不能把所有代碼放在這里,但它意味着繞過一個錯誤我得到的時候我這樣實現定義

$containerBuilder->addDefinitions([
'auth' => \DI\object('MyProject\Users\Handlers\Permissions')->lazy()
]);

錯誤是:

致命錯誤:帶有消息'ContainerBuilder :: addDefinitions()參數的未捕獲異常'InvalidArgumentException'必須是字符串或實現ChainableDefinitionSource,給定數組

感謝您及時的回復。

鑒於您的上次編輯,您似乎仍在使用PHP-DI 4。

添加數組的功能已添加到5.0.0版: https//github.com/PHP-DI/PHP-DI/issues/218

作為附錄,如果無法完全解析定義,則has()函數返回FALSE值。 例如,從我的例子中可以看出:

$containerBuilder->addDefinitions([
'auth' => \DI\object('MyProject\Users\Handlers\Permissions')->lazy()
]);

如果未完全解析類MyProject \\ Users \\ Handlers \\ Permissions

$ container-> has('auth')//這將返回一個FALSE布爾值

希望這有助於某人

暫無
暫無

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

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