Lazyloading Symfony services

Code
<?php

use Symfony\Component\DependencyInjection\ContainerBuilder;
use 
Symfony\Component\DependencyInjection\Definition;
use 
Symfony\Component\DependencyInjection\Reference;
use 
Symfony\Bridge\ProxyManager\LazyProxy\Instantiator\RuntimeInstantiator;

class 
Foo
{
    public function 
__construct()
    {
        echo 
'foo constructor' PHP_EOL;
    }
    
    public function 
getFooString()
    {
        return 
'foo';
    }
}

class 
Bar
{
    private 
$foo;
    
    public function 
__construct(Foo $foo)
    {
        
$this->foo $foo;
    }
    
    public function 
getOutput()
    {
        echo 
'before foo is used' PHP_EOL;
        
$output 'bar' $this->foo->getFooString();
        echo 
'after foo is used' PHP_EOL;
        
        return 
$output;
    }
}


// the service container
$fooDefinition = new Definition('Foo');
// set foo as lazy
$fooDefinition->setLazy(true);

$barDefinition = new Definition('Bar', [$fooDefinition]);

$containerBuilder = new ContainerBuilder();
$containerBuilder->setProxyInstantiator(new RuntimeInstantiator());
$containerBuilder->setDefinition('foo'$fooDefinition);
$containerBuilder->setDefinition('bar'$barDefinition);


// call the new bar service with foo dependency
$bar $containerBuilder->get('bar');
$bar->getOutput();
Result
before foo is used
foo constructor
after foo is used
Used Versions
PHP 7.1, Zend 3.0, Symfony 3.2, Laravel 5.2, PHPUnit 5.6, Doctrine 2.6