symfony/symfony — Issue #2393: Better Support for Injecting Into Existing Objects
Author: schmittjoh
Created: 2011-10-13 21:40:12+00:00
State: open
Labels: Enhancement, Service Container

We should add better support for injecting dependencies into existing objects that are not created by the DIC.

```php
wire($obj); // inject dependencies
```

The main use case I have in mind for this are entities. If they are hydrated from the database, we could hook into a lifecycle event to inject dependencies. When they are created by the user, he could manually call the code above to have dependencies injected.

Configuration for this could be possible on a per class basis, or also via a @Configurable annotation (similar to Spring with the only difference, that in PHP we cannot hook into object construction, but the user has to call ``->wire()`` manually).

Injections into entities, although in some cases used for the wrong reasons, is still very useful in certain cases and a commonly requested feature.