You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Extend a previous object definition to add method calls #349
DI\object()->method() behaves somewhat unexpected when extending objects.
Here is an example:
// definitions1.phpreturn [
MyClass::class=>DI\object()
->method('setConfiguration', 'a config value')
->method('addSomething', 'something')
->method('addSomething', 'another something')
];
// definitions2.phpreturn [
MyClass::class=>DI\object()
->method('setConfiguration', 'a new config value')
->method('addSomething', 'just another something')
];
My expectation was, that "addSomething" is called three times but it is not. In fact the first call with "something" is replaced with "just another something". I know that this is great if you set something and want to override the value later. But what is the right way to explicitly add a method call?
Hi, thanks for the detailed report. This is indeed a shortcoming of the current behavior, which is meant to allow to override (or rather extend) previous definitions. The reason it works like that today is mostly for historic reasons: until 5.1 you couldn't even call twice the same method, so the only use case was to override parameters for the method call.
Now that you can call the same method several times, this flaw has become more obvious… :(
This is something I want to change for PHP-DI 6. Actually it might not even need a new major version since I want to preserve backward compatibility for object(), I might create a new DI\create() helper that will eventually replace object() (after being deprecated). The behavior of create() would be to not extend anything. Extending would be something different (to be defined).
Now to answer directly your question:
what is the right way to explicitly add a method call?
With object(), it's not possible unfortunately. What you can do as a workaround:
Activity
mnapoli commentedon Dec 7, 2015
Hi, thanks for the detailed report. This is indeed a shortcoming of the current behavior, which is meant to allow to override (or rather extend) previous definitions. The reason it works like that today is mostly for historic reasons: until 5.1 you couldn't even call twice the same method, so the only use case was to override parameters for the method call.
Now that you can call the same method several times, this flaw has become more obvious… :(
This is something I want to change for PHP-DI 6. Actually it might not even need a new major version since I want to preserve backward compatibility for
object(), I might create a newDI\create()helper that will eventually replaceobject()(after being deprecated). The behavior ofcreate()would be to not extend anything. Extending would be something different (to be defined).Now to answer directly your question:
With
object(), it's not possible unfortunately. What you can do as a workaround:At least that's possible until a better solution.
[-]DI\object()->method() and extending objects[/-][+]Extend a previous object definition to add method calls[/+]mnapoli commentedon Apr 15, 2017
This has been fixed in #449 for PHP-DI 6 (which is work in progress).