Aura DI lazy function called each time

24 views
Skip to first unread message

Laurens Verspeek

unread,
Jan 17, 2017, 1:34:41 PM1/17/17
to The Aura Project for PHP
I have a config service and a pdo service, but I only want to set the pdo service based on some values in the config service.
However, when using the lazy function, each time I call $di->get('pdo') the function to check whether to set the pdo or not (the anonymous function in the lazy function) is executed. How can I make sure this function is only called once? Example code:

 $di->set('config', $di->lazyNew(Config::class));

 $di->set('pdo', $di->lazy(function () use ($di) {
     $config = $di->get('config');
     if($config->database){
         return $di->lazyNew(PdoPlus::class);
     } else {
         return null;
     }
 }));

$di->get('pdo'); // configuration is checked
$di->get('pdo'); // configuration is checked again :(


Hari K T

unread,
Jan 17, 2017, 11:45:52 PM1/17/17
to aur...@googlegroups.com
Hi, 

I have tried your example over .


I don't see it getting executed twice. If you still feel the same, may be you could send a PR with a failing test ?

Thank you

Laurens Verspeek

unread,
Jan 18, 2017, 10:03:50 AM1/18/17
to The Aura Project for PHP
I think I found the bug:

When you return null in the lazy function, it will run the function again when you resolve the service again.
So it will print 'Called once ?' twice when null is returned. You can try it out by removing line 10 in your code ($this->database = 'yes';)

Paul Jones

unread,
Jan 18, 2017, 10:09:32 AM1/18/17
to aur...@googlegroups.com

> On Jan 18, 2017, at 09:02, Laurens Verspeek <l.ver...@gmail.com> wrote:
>
> I think I found the bug:
>
> When you return null in the lazy function, it will run the function again when you resolve the service again.

Ah so. Yes, the service remains "unset" if it contains null, and so the container considers the service un-instantiated.

Perhaps the thing to do is throw an exception if the factory does not return an object ... ?


--
Paul M. Jones
pmjo...@gmail.com
http://paul-m-jones.com

Modernizing Legacy Applications in PHP
https://leanpub.com/mlaphp

Solving the N+1 Problem in PHP
https://leanpub.com/sn1php



Laurens Verspeek

unread,
Jan 18, 2017, 2:35:14 PM1/18/17
to The Aura Project for PHP
> Perhaps the thing to do is throw an exception if the factory does not return an object ... ? 
For consistency this is probably the best thing to do ($di->set('service', null) also throws an exception stating that null is not an object). However, does a service have to be an object? Why can't a service be null?
And if a service can't be null, how can I solve my problem? I only need to set the pdo service if the database information is specified in my config service..

Hari K T

unread,
Jan 18, 2017, 7:28:01 PM1/18/17
to aur...@googlegroups.com
For consistency this is probably the best thing to do ($di->set('service', null) also throws an exception stating that null is not an object). However, does a service have to be an object? Why can't a service be null?
And if a service can't be null, how can I solve my problem? I only need to set the pdo service if the database information is specified in my config service..

Are you not saving the data with pdo then ?

What is the logic behind setting pdo to null ?

Laurens Verspeek

unread,
Jan 19, 2017, 7:52:47 AM1/19/17
to The Aura Project for PHP
I only want to save the data with PDO when the database settings are specified in the config service, otherwise I don't want to use the PDO service. So I either don't want to set the pdo service at all or let the pdo service be null.

Because I need to resolve the config service first before I know whether or not I need a PDO service, I have to use the lazy function to set the pdo service according to settings in the config service.

Hari K T

unread,
Jan 19, 2017, 9:31:28 AM1/19/17
to aur...@googlegroups.com
Hi, 

One fix for the current time is you can return a stdClass object and check the instanceof($service) and do the rest if it is Pdo .

Laurens Verspeek

unread,
Jan 19, 2017, 4:01:52 PM1/19/17
to The Aura Project for PHP
Even though it works, it really feels like a hacky fix.. Isn't there a better way to do it?

Hari K T

unread,
Jan 20, 2017, 12:27:53 AM1/20/17
to The Aura Project for PHP
Hi, 
 
Even though it works, it really feels like a hacky fix.. Isn't there a better way to do it?

Not I am aware of :-( .

Sorry for the trouble. 
Reply all
Reply to author
Forward
0 new messages