New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend a previous object definition to add method calls #349

Closed
mopahle opened this Issue Dec 7, 2015 · 2 comments

Comments

Projects
2 participants
@mopahle
Contributor

mopahle commented Dec 7, 2015

DI\object()->method() behaves somewhat unexpected when extending objects.
Here is an example:

// definitions1.php
return [
    MyClass::class=>DI\object()
        ->method('setConfiguration', 'a config value')
        ->method('addSomething', 'something')
        ->method('addSomething', 'another something')
];

// definitions2.php
return [
    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?

@mnapoli

This comment has been minimized.

Member

mnapoli commented 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 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:

// definitions1.php
return [
    MyClass::class=>DI\object()
        ->method('addSomething', 'something')
        ->method('addSomething', 'another something')
];

// definitions2.php
return [
    MyClass::class=>DI\ decorate(function (MyClass $obj) {
        $obj->addSomething('just another something');
        return $obj;
    })
];

At least that's possible until a better solution.

@mnapoli mnapoli changed the title from DI\object()->method() and extending objects to Extend a previous object definition to add method calls Dec 7, 2015

@mnapoli mnapoli added the enhancement label Dec 7, 2015

@mnapoli mnapoli moved this from Planned to In Progress in PHP-DI v6 Mar 22, 2017

@mnapoli mnapoli referenced this issue Mar 22, 2017

Merged

Refactor object() to create() and autowire() #449

10 of 10 tasks complete

@mnapoli mnapoli added this to the 6.0 milestone Apr 15, 2017

@mnapoli

This comment has been minimized.

Member

mnapoli commented Apr 15, 2017

This has been fixed in #449 for PHP-DI 6 (which is work in progress).

@mnapoli mnapoli closed this Apr 15, 2017

@mnapoli mnapoli moved this from In Progress to Done in PHP-DI v6 Apr 15, 2017

@mnapoli mnapoli self-assigned this Apr 17, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment