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 {#1491
-storage: array:1 [
"395d819f44281b6727e133819c1a91f5" => ""
]
flag::STD_PROP_LIST: false
flag::ARRAY_AS_PROPS: false
iteratorClass: "ArrayIterator"
}

^ ArrayObject {#1491
-storage: array:2 [
"395d819f44281b6727e133819c1a91f5" => ""
"7c2e6352975956b08724b8ac20c7f62e" => ""
]
flag::STD_PROP_LIST: false
flag::ARRAY_AS_PROPS: false
iteratorClass: "ArrayIterator"
}
Used Versions
PHP 8.2, Laminas MVC 3.2, Symfony 5.2, Laravel 8.28, PHPUnit 9.5, Doctrine ORM 2.8