简体   繁体   中英

Symfony 2 Dependency Injection & autowiring

I'm browsing the Symfony 2 docs related to Dependency Injection , and can't find a reference to autowiring. I found a bundle that offers some of this functionality , but it's still in beta and seems to be tied to annotations (correct me if I'm wrong).

What I'm looking for is an object (such as the service container), that could inject dependencies in my services, via setter injection.

For example, I would define a Service:

class Service {
    /**
     * @var \PDO
     */
    protected $pdo;

    /**
     * @param \PDO $pdo
     * @Inject
     */
    public function setPDO(\PDO $pdo) {
        $this->pdo = $pdo;
    }
}

And then, I could use this hypothetical service container to inject dependencies in the Service, even if this one has been created outside the container:

$service = new Service();
// ...

$container->inject($service);

Is there a DI container that could autowire dependencies this way?

从Symfony 2.8开始,本机支持自动装配: https//github.com/symfony/symfony/pull/15613

还可以通过https://github.com/kutny/autowiring-bundle获得自动装配包。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM