4

PHP-DI allows some methods to define injections, including factories and objects: http://php-di.org/doc/php-definitions.html.

Factories:

TestClass::class => function () {
   return new TestClass('param');
}

The TestClass instance is created lazily, only when it is needed.

Objects:

TestClass::class => DI\object()->constructor('param')

If you use Objects, is the instance also created lazily?

If yes, what's the difference between factories and objects?

Bob
  • 159
  • 7
  • Reading the documentation seems to hint that objects are created during initialisation, not on demand – Phil Aug 20 '17 at 23:15
  • @Phil I thought the same thing. But in this case, why use objects when you can use factories that only create a class instance when it's needed? – Bob Aug 20 '17 at 23:33

1 Answers1

4

PHP-DI author here, it seems there is some confusion (given the question and how wrong the other answer is). I have improved the documentation, hopefully that will clear things up: ec8120ee.

To answer your questions:

If you use Objects, is the instance also created lazily?

Yes, all definitions are resolved lazily, object() too.

If yes, what's the difference between factories and objects?

Just the syntax. In some cases it's more practical to write a closure, in some other cases you may want to avoid the boilerplate by using object().

It's just a syntax preference honestly.

Matthieu Napoli
  • 48,448
  • 45
  • 173
  • 261
  • This still isn't completely clear to me. You are saying that closure is the same thing as factory, and this does not clarify what the difference between DI\factory() and DI\object() is. – avatarofhope2 Nov 21 '17 at 22:40
  • @avatarofhope2 this is similar to the difference between `echo $foo` and `print $foo` : it's the same way to do something but with a different syntax. `object()` is useful if you want to avoid having to write a closure or a factory class (`object() will call `new YourClass`). `factory()` is useful if you like writing closures, or if you have a factory class (for example `LoggerFactory::createLoggerInstance()`). – Matthieu Napoli Nov 23 '17 at 07:17
  • That echo and print analogy did not seem perfect because they have similar signatures. After looking at the source for object() and factory(), the arguments are different. object() takes a string, which is the name of the class, and factory takes only a callable. I now understand the difference though. – avatarofhope2 Nov 28 '17 at 19:49
  • OK then it's similar to what you can do with `date()` and with `DateTime` (manipulate dates, but with a different API). – Matthieu Napoli Nov 29 '17 at 14:27
  • I was wondering about this, saw the docs for factories and got a bit confused as to what the difference was with a standard definition. I found it a bit confusing because I thought the factory method might return a callable which could then be called with an argument like you would a factory method instead of just another way of creating a definition. Thanks for clearing that up, I think the docs lack examples of actually using the definitions, that would have made it a bit clearer I think. – alvinc Feb 04 '21 at 19:36