Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Injecting dependencies using annotations in Symfony

I come from a Spring/Java background and i'm learning Symfony/PHP. It's amazingly similar and i love it.

In Spring we can @Autowire dependencies. What is the equivalent of that in Symfony? I want to inject my dependencies using annotations. And i dont want to specify that in an xml or yml.

For example:

class foo {

        /**
         * @Inject \ABC\XYZ\Dependency
         */
        private $dependency;

        public function abc(){
              $dependency->bar();
        }
};

Also (now this just got on my mind now), is it possible to do something like this. Using annotations, declaring a name and scope for the service:

/**
 * @Service("someService")
 * @Scope("session / request / ..")
 */
class foo {

        /**
         * @Inject \ABC\XYZ\Dependency
         */
        private $dependency;

        public function abc(){
              $dependency->bar();
        }
};
like image 300
Chinmay Avatar asked Apr 27 '26 06:04

Chinmay


1 Answers

Autowiring will be available as of Symfony 2.8: http://symfony.com/blog/new-in-symfony-2-8-service-auto-wiring

like image 195
Thomas K Avatar answered Apr 28 '26 21:04

Thomas K



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!