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?
The text was updated successfully, but these errors were encountered:
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:
DI\object()->method() behaves somewhat unexpected when extending objects.
Here is an example:
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?
The text was updated successfully, but these errors were encountered: