简体   繁体   中英

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

Using a php definition file I have created this definition

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

But when I use the has() function to check if the definition exists ie

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

but the get() function manages to return the object.

$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

Thanks for the quick response.

Given your last edit it seems you are still using PHP-DI 4.

The ability to add arrays was added to version 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. 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

$container->has('auth') //this will return a FALSE boolean value

Hope this helps someone

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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