The best way to find out is to try it:
1). Install a Symfony standard edition application
I installed Symfony 2.8.1 for this example.
2). In the default AppBundle add the following files in Services
folder:
ServiceA.php
:
namespace AppBundle\Services;
class ServiceA
{
private $service;
public function __construct(ServiceB $service)
{
$this->service = $service;
}
}
ServiceB.php
:
namespace AppBundle\Services;
class ServiceB
{
private $service;
public function __construct(ServiceA $service)
{
$this->service = $service;
}
}
3). Add an extension subclass in DependencyInjection
folder:
AppExtension.php
:
namespace AppBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader;
class AppExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources'));
$loader->load('services.yml');
}
}
4). And declare the circular-dependent services in Resources
folder:
services.yml
:
services:
service_a:
class: AppBundle\Services\ServiceA
arguments:
- @service_b
service_b:
class: AppBundle\Services\ServiceB
arguments:
- @service_a
After all this, if you run php app/console --version
, you will get the following error:
[Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException]
Circular reference detected for service "service_a", path: "service_a -> service_b -> service_a".