1

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 i.e.

$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.

Stanley Ngumo
  • 4,089
  • 8
  • 44
  • 64

2 Answers2

0

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

Matthieu Napoli
  • 48,448
  • 45
  • 173
  • 261
0

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

Stanley Ngumo
  • 4,089
  • 8
  • 44
  • 64