简体   繁体   English

在PHP-DI中,has()函数不适用于文件中的定义

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

Using a php definition file I have created this definition 使用php定义文件我创建了这个定义

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

But when I use the has() function to check if the definition exists ie 但是当我使用has()函数来检查定义是否存在时

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

but the get() function manages to return the object. 但是get()函数设法返回对象。

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

EDIT: The application is a bit complex so cant put all the code here but its meant to bypass an error Im getting when I implement the definitions this way 编辑:应用程序有点复杂,所以不能把所有代码放在这里,但它意味着绕过一个错误我得到的时候我这样实现定义

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

The error is: 错误是:

Fatal error: Uncaught exception 'InvalidArgumentException' with message 'ContainerBuilder::addDefinitions() parameter must be a string or implement ChainableDefinitionSource, array given 致命错误:带有消息'ContainerBuilder :: addDefinitions()参数的未捕获异常'InvalidArgumentException'必须是字符串或实现ChainableDefinitionSource,给定数组

Thanks for the quick response. 感谢您及时的回复。

Given your last edit it seems you are still using PHP-DI 4. 鉴于您的上次编辑,您似乎仍在使用PHP-DI 4。

The ability to add arrays was added to version 5.0.0: https://github.com/PHP-DI/PHP-DI/issues/218 添加数组的功能已添加到5.0.0版: https//github.com/PHP-DI/PHP-DI/issues/218

And as an addendum, the has() function returns a FALSE value if the definition cannot be fully resolved. 作为附录,如果无法完全解析定义,则has()函数返回FALSE值。 For example from my example below: 例如,从我的例子中可以看出:

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

If the class MyProject\\Users\\Handlers\\Permissions is not fully resolved 如果未完全解析类MyProject \\ Users \\ Handlers \\ Permissions

$container->has('auth') //this will return a FALSE boolean value $ container-> has('auth')//这将返回一个FALSE布尔值

Hope this helps someone 希望这有助于某人

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM