Symfony3 Auto Wiring – Auto Creating Dependency Services

Previously if you ever had dependency for your object, you had to make sure the names for dependency services are correctly passed down to your new service before you can start using it.

Since Symfony 2.8 which is official exact version of Symfony 3 (removing all the deprecated functions).

Now you can create services without worrying about dependency injection services with auto wiring feature. The key feature in auto wiring is, if one of your dependency injection service does not exist, then it will auto create it and inject it.

Let’s assume we have following object, which needs to be created as service.
namespace AppBundle\Service;

use AppBundle\Service\House;

class Room
{
    private $house;

    public function __construct(House $house)
    {
        $this->house = $house;
    }
}

Old way creating service
services:
    house:
        class: AppBundle\Service\House

    room:
        class: AppBundle\Service\Room
        arguments:
            - @house
New way with auto wiring
services:
    room:
        class: AppBundle\Service\Room
        autowire: true

The above example is very simple. Now imagine you have 5 or even 6 different dependency injections, you would have to waste time on getting each dependency service names and on top, if you later on decided to change the or remove dependency from your object, then you have to update services as well. With auto wiring you don’t have to worry about anything, it worries for you.

What about Performance?

It is exactly as same as before. Compiler builds and save everything in cache, just like before.

Leave a Reply

Your email address will not be published. Required fields are marked *