Heya! Using a service locator is very common approach to handle this. You basically do following:
class MyLazyObject { protected $sl; protected $prop; public function __construct(Zend\ServiceManager\ServiceLocatorInterface $sl) { $this->sl = $sl; } public function doStuff() { if (null === $this->prop) { $this->prop = $this->sl->get('some-stuff') } return $this->prop->otherStuff(); } } Eventually, you may check out what I'm trying to achieve at https://github.com/Ocramius/OcraServiceManager, which basically makes your objects lazy when they're marked to behave like that. I described extensively (some time ago) what seems to be your aim at http://ocramius.github.com/blog/zf2-and-symfony-service-proxies-with-doctrine-proxies/ Marco Pivetta http://twitter.com/Ocramius http://ocramius.github.com/ On 14 December 2012 12:56, Daniel Latter <dan.lat...@gmail.com> wrote: > Hi All, > > I just have a question regarding injecting dependencies and lazy loading. > > The way I have developed my objects (model classes) is to pass in an array > of data to the constructor, this mainly hanldes the setting of scalar > properties (strings, int's etc) on the object, > when I wish to load a property that is an object my aim is to lazy load it > on demand. > > What is the best way to accomplish this? > > I am going to do this via setter injection in the model, so my first > thought is to use a DiC, > accessible via model classes, is this a common approach? > Or I was also thinking about creating a lazy_load method that would be > utilized by model getter methods: > > Something like so: > > .... > public function getProp() > return lazy_load($this->prop, $className, $args); > } > ... > > Then inside the lazy load function, I could harness the DiC? > > Any comments or improvements on this approach? > > Many Thanks > Daniel >