繁体   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