Injecting Services in Symfony 2

Hi all,

Let’s have a quick but precise look at injecting various services in Symfony 2 today. Before going any deep, what does it mean by injecting services ? Well, if I am not so wrong, not many PHP frameworks do favour injecting services as Symfony 2 does. Injecting various services into the application is a really beautiful, cool thing, I would say. If you manage to inject services properly, you really don’t want to take care of object creations. Well, what’s injecting service actually is ? Injecting a service in Symfony 2 is, you define, a service or set of services (in PHP, classes) at the application load time, then you reuse those services as much as you want, any time during the execution, without redeclaring or creating objects of those services (classes). In other words it will help you to “standardize and centralize the way objects are constructed in your application” . There are 3 types of injections in Symfony 2, which are,

  1. Constructor Injection
  2. Setter Injection
  3. Property Injection

In this post, I will be discussing about the ‘Constructor Injection’, because I thought it might be useful to know how this type of injection works before knowing the remaining two.  Continue reading