PhpRiot
News Archive
PhpRiot Newsletter
Your Email Address:

More information

Kevin Schroeder's Blog: ZF2 Dependency Injection - Multiple Object Instances

Note: This article was originally published at PHPDeveloper on 1 May 2012.
PHPDeveloper

Kevin Schroeder has a quick new post about using dependency injection in Zend Framework 2 applications using multiple object instances.

When you work with the ZF2 Dependency Injection Container (DiC) when you make multiple requests for an instance of an object you will get the same object back each time. [...] But what if you want the injection benefits of the DiC but don't want to share the object? Use the DiC's newInstance method instead with the third parameter being false.

He includes code examples of requesting the object both ways - the usual way that returns the same object and the alternative that passes in a "false" value, complete with a debug output of each object proving they're different.