Symfony services and autowire. Get dependency for a service by a class' type hint.

Code
<?php

use Symfony\Component\DependencyInjection\ContainerBuilder;
use 
Symfony\Component\DependencyInjection\Definition;

class 
Foo
{
    private 
$ao;
    
    public function 
__construct(ArrayObject $bar)
    {
        
$this->ao $bar;
    }
    
    public function 
getObject()
    {
        
$this->ao[md5(microtime())] = '';
        return 
$this->ao;
    }
}

$arrDefinition = new Definition('ArrayObject');
$fooDefinition = new Definition('Foo');
// autowired: get dependency by type hint of Foo class
$fooDefinition->setAutowired(true);

// set definitions in container builder
$containerBuilder = new ContainerBuilder();
$containerBuilder->setDefinition('foo'$fooDefinition);
$containerBuilder->setDefinition('array_object'$arrDefinition);
$containerBuilder->compile();

// calling foo service
dump($containerBuilder->get('foo')->getObject());
dump($containerBuilder->get('foo')->getObject());
Result
ArrayObject {#428
flag::STD_PROP_LIST: false
flag::ARRAY_AS_PROPS: false
iteratorClass: "ArrayIterator"
storage: array:1 [
"b20e1296143ee151cc706589bc1c3259" => ""
]
}

ArrayObject {#428
flag::STD_PROP_LIST: false
flag::ARRAY_AS_PROPS: false
iteratorClass: "ArrayIterator"
storage: array:2 [
"b20e1296143ee151cc706589bc1c3259" => ""
"b61e02f9352d3e963b82bfa46089c8c4" => ""
]
}
Used Versions
PHP 7.1, Zend 3.0, Symfony 3.2, Laravel 5.2, PHPUnit 5.6, Doctrine 2.6