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');
$fooDefinition->setPublic(true);
// 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('ArrayObject'$arrDefinition);
$containerBuilder->compile();

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

ArrayObject {#467
flag::STD_PROP_LIST: false
flag::ARRAY_AS_PROPS: false
iteratorClass: "ArrayIterator"
storage: array:2 [
"d7f69ac83471947bee316c5207004aec" => ""
"483b8a0c8f6c5df78210fdb2fa5d7e62" => ""
]
}
Used Versions
PHP 7.2, Zend 3.0, Symfony 4.0, Laravel 5.6, PHPUnit 6.5, Doctrine 2.8