Dependency Injection: Circular References

262 views
Skip to first unread message

Michael Ridgway

unread,
Nov 11, 2010, 5:00:37 PM11/11/10
to Symfony Components
I'm using Symfony's DI container and I have a circular dependency.
The container throws 'The service has a circular reference to
itself.' If the dependency is injected using a setter, shouldn't this
be a non-issue? Looking at the code that was generated:

protected function getAService()
{
if (isset($this->shared['a'])) return $this->shared['a'];

$class = 'Service\\A';
$instance = new $class();
$instance->setBService($this->getService('b'));

return $this->shared['a'] = $instance;
}

protected function getBService()
{
if (isset($this->shared['b'])) return $this->shared['b'];

$class = 'Service\\B';
$instance = new $class();
$instance->setAService($this->getService('a'));

return $this->shared['b'] = $instance;
}

the issue is apparent. The shared[] key is not set to the instance
until all of the dependencies have been injected. I imagine this was
done on purpose, but I'm interested in the insight to this.

Fabien Potencier

unread,
Nov 13, 2010, 8:47:34 AM11/13/10
to symfony-c...@googlegroups.com
I think that this issue has been fixed in the PHP 5.3 version of the
DIC. Which version do you use?

--
Fabien Potencier
Sensio CEO - symfony lead developer
sensiolabs.com | symfony-project.org | fabien.potencier.org
Tél: +33 1 40 99 80 80

Michael Ridgway

unread,
Nov 13, 2010, 9:27:23 AM11/13/10
to symfony-c...@googlegroups.com
We are using the version that is on https://github.com/fabpot/dependency-injection

Looking around I didn't find an independent download of the 5.3 version.  Is it only available as a part of the symfony2 framework right now?  Also is this version considered stable?


Michael Ridgway


--
You received this message because you are subscribed to the Google Groups "Symfony Components" group.
To post to this group, send email to symfony-c...@googlegroups.com.
To unsubscribe from this group, send email to symfony-compone...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/symfony-components?hl=en.


Fabien Potencier

unread,
Nov 14, 2010, 5:27:09 AM11/14/10
to symfony-c...@googlegroups.com
On 11/13/10 3:27 PM, Michael Ridgway wrote:
> We are using the version that is on
> https://github.com/fabpot/dependency-injection
>
> Looking around I didn't find an independent download of the 5.3
> version. Is it only available as a part of the symfony2 framework right
> now? Also is this version considered stable?

This is part of the Symfony2 framework but can be used standalone. It is
more stable than the PHP 5.2 one.

Fabien

>
> Michael Ridgway
>
>
> On Sat, Nov 13, 2010 at 8:47 AM, Fabien Potencier
> <fabien.p...@symfony-project.com

> <mailto:fabien.p...@symfony-project.com>> wrote:
>
> I think that this issue has been fixed in the PHP 5.3 version of the
> DIC. Which version do you use?
>
> --
> Fabien Potencier
> Sensio CEO - symfony lead developer

> sensiolabs.com <http://sensiolabs.com> | symfony-project.org
> <http://symfony-project.org> | fabien.potencier.org
> <http://fabien.potencier.org>

> <mailto:symfony-c...@googlegroups.com>.


> To unsubscribe from this group, send email to
> symfony-compone...@googlegroups.com

> <mailto:symfony-components%2Bunsu...@googlegroups.com>.

Reply all
Reply to author
Forward
0 new messages