How to lazy load objects in PHP
Tags: experience, php
Published: 2024-03-16 10:08:29

---
title: How to lazy load objects in PHP
published: true
date: 2015-07-21 11:55:15 UTC
tags: Experience,PHP
canonical_url: https://io.serendipityhq.com/experience/how-to-lazy-load-objects-in-php/
---

 **Lazy loading is a mechanism that make the script able to load data only when they are required to.**

So, if we have to load a big graph of data, we don’t load it entirely at the start, but only when it is required for use.

This mechanism is very useful as it maintain the application performances high (if used with cause cognition!).

Start implementing this mechanism in our apps could be really complex, especially if we have no confidence with some patterns that are behind it, as the Proxy or Observer patterns are.

**The really important thing to remember in approaching those kind of tasks is the [Liskov Substitution Principle](https://en.wikipedia.org/wiki/Liskov_substitution_principle)**: in simple words, obj `A` and obj `AProxy` have to be used in the same context without breaking the application as the one can be substituted with the other.

**There are [3 main kind of objects](http://www.slideshare.net/Ocramius/proxy-ooppatterninphp) we can use to implement lazy loading:**

- **Virtual objects** : An object whose properties are all set to `null` and where each access to the properties is tracked and, once an access is intercepted, the lazy loading is triggered to get the current real value of the property;
- **Value Holder** : holds an instance of the original proxied object and loads the real object (hydrated with real values) only when needed (this can be something different from a [Value Object](http://aerendir.me/?p=396));
- **Ghost objects** : An object whose properties are the same of the proxied object, but `null`. Accessing any method causes loading of the properties (this is the mechanism used by Doctrine lazy loading implementation).

There are some rules to follow implementing the Proxy Object:

> 1. The Proxy class MUST extend the proxied class
> 2. Each of the proxied methods must be rewritten
> 3. Proxies should be serializable
> 4. Proxies should handle public properties
> <footer>Ocramius about <cite title="Ocramius about Proxy OOP Pattern in PHP"><a href="http://www.slideshare.net/Ocramius/proxy-ooppatterninphp" target="_blank" rel="noopener">Proxy OOP Pattern in PHP</a></cite></footer>

An example of a lazy loadable object generated by Doctrine:

Here some useful links about lazy loading in PHP.

- A very complete view on [objects types that can be used to implement the lazy loading functionalities](http://ocramius.github.io/presentations/proxy-pattern-in-php/#/1) (by Marco “Ocramius” Pivetta);
- Another slide deck about the [objects types that can be used to implement the lazy loading functionalities](http://www.slideshare.net/Ocramius/proxy-ooppatterninphp) (ever by Marco “Ocramius” Pivetta);
- A question on StackOverflow about [How does the Doctrine Repository mechanism of lazy loading entities work](http://stackoverflow.com/q/31182044/1399706);
- How does it was implemented in Doctrine 2 (by Giorgio Sironi): [Before the implementation](http://www.giorgiosironi.com/2009/07/lazy-loading-of-objects-from-database.html) and [After the implementation](http://www.giorgiosironi.com/2009/08/doctrine-2-now-has-lazy-loading.html);
- A further [explanation of the Doctrine Proxy implementation](http://ocramius.github.io/blog/zf2-and-symfony-service-proxies-with-doctrine-proxies/) (by Marco “Ocramius” Pivetta);
- How to implement the Proxy Pattern: [Example 1](http://www.tutorialspoint.com/design_pattern/proxy_pattern.htm) and [Example 2](http://www.giorgiosironi.com/2010/02/practical-php-patterns-proxy.html) (lazy loading of images properties), [Example 3](https://sourcemaking.com/design_patterns/proxy/php) (retrieving lazily a list of books);
- Something [about the Observer pattern](https://sourcemaking.com/design_patterns/observer/php);
- [The Proxy Manager I’m using](http://ocramius.github.io/ProxyManager/) (compatible with both Zend Framework 2 and Symfony Framework 2);

Remember to “Make. Ideas. Happen.”.

I wish you flocking users, see you soon!

L'articolo [How to lazy load objects in PHP](https://io.serendipityhq.com/experience/how-to-lazy-load-objects-in-php/) proviene da [ÐΞV Experiences by Serendipity HQ](https://io.serendipityhq.com).