8000 [DI] Support getter-based dependency injection · Issue #20657 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
[DI] Support getter-based dependency injection #20657
Closed
@nicolas-grekas

Description

@nicolas-grekas

A concept well described in e.g. this thread:
http://www.theserverside.com/news/thread.tss?thread_id=26205
and also in #835

Basically, one could then be able override the getConnection method of such a class:

class foo {
// ...
    public/protected function getConnection()
    {
        return new FooDefaultConnection();
    }
}

with a generated proxy ($this->container auto-injected by some to-be-defined mean):

class extends foo {
    private $container;

    public/protected function getConnection()
    {
        return $this->container('connection_for_foo'):
    }
}

in XML, this could defined with something like e.g.:

<service id="foo" class="foo">
<getter name="getConnection" type="service" id="connection_for_foo" />
</service>

so that $container->get('foo') returns an instance of the generated class (and by this mean an instance of foo of course).

The benefits?

  • Lazyness made simple and almost free! I may give it a try;
  • immutability (if no corresponding setter exists of course)

Metadata

Metadata

Assignees

No one assigned

    Labels

    DependencyInjectionRFCRFC = Request For Comments (proposals about features that you want to be discussed)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0