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 [
"ad60f20d4fc7a8ecd2cae851bdb89538" => ""
]
}

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