Symfony Dependency Injection inject new instance of class

Scopes have been deprecated since 2.8. Use shared: false instead.

http://symfony.com/doc/current/cookbook/service_container/shared.html

services:
  session:
    class: Vendor\Core\Session
    arguments: [ "@inject.me" ]

  inject.me:
    class: Class\To\Inject
    shared: false

I now that this question is pretty old but here is a trick that you could use to avoid define every simple class as service defining a "factory class" service that receive the class to create as argument and after through "expression language" inject as argument:

<?php

final class ClassFactory
{
   public static function create($class, array $arguments = [])
   {
     return new $class($arguments);
   }
}
  • create the new service

    app.class_factory: class: ClassFactory

  • after inject the new classes like that:

    arguments: ['@=service("app.class_factory").create("Monolog\Logger")']

For Symfony >=2.8 you can take a look to the "auto-wiring" feature also -> http://symfony.com/blog/new-in-symfony-2-8-service-auto-wiring