Skip to content

Extend a previous object definition to add method calls #349

Closed

Description

@mopahle

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?

Activity

  1. mnapoli commented on Dec 7, 2015

    @mnapoli
    Member

    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.

  2. changed the title [-]DI\object()->method() and extending objects[/-] [+]Extend a previous object definition to add method calls[/+] on Dec 7, 2015
  3. added this to the 6.0 milestone on Apr 15, 2017
  4. mnapoli commented on Apr 15, 2017

    @mnapoli
    Member

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

  5. self-assigned this
    on Apr 17, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions