Lazy loading
Learn lazy loading approaches that load data only when needed.
What is lazy loading?
Lazy loading means fetching data only when it is actually needed. Instead of loading right away you hold a reference; on first access you prepare the data, cache it, and serve later accesses from cache. This is efficient for large data that may never be used — but it can lead to the N+1 problem.
The lazy initialization pattern is the common form: a property is null at first, loads on demand, returns the cached value afterwards. In PHP you implement it either as a class property or with a captured closure.
A lazy initialization class
Lazy initialization looks like this in a class: a property starts as null (private ?array $data = null;), the get method checks === null, loads and fills it on first call, and returns it directly afterwards.
The loading cost is paid once; later calls are nearly free. This pattern fits configuration, translations and database lookups. Note: a lazy property does not invalidate once loaded, so you must reset it manually if the source changes.
Exercise Write the LazyConfig class
Write a LazyConfig class with private ?array $data = null;. The get(string $key): ?string method: when $this->data === null fill $this->data with ['name' => 'app', 'version' => '1.0']. Return $this->data[$key] ?? null. Create $config = new LazyConfig();. Call get('name'), get('version') and get('missing'). Print the first two with echo; for the third print '0' if null else '1'.
Show example solution
?string { if ($this->data === null) { $this->data = ['name' => 'app', 'version' => '1.0']; } return $this->data[$key] ?? null; } }. $config = new LazyConfig(); echo $config->get('name') . "\n"; echo $config->get('version') . "\n"; echo ($config->get('missing') === null ? '0' : '1') . "\n";.
Example solution:
<?php
class LazyConfig {
private ?array $data = null;
public function get(string $key): ?string {
if ($this->data === null) {
$this->data = ['name' => 'app', 'version' => '1.0'];
}
return $this->data[$key] ?? null;
}
}
$config = new LazyConfig();
echo $config->get('name') . "\n";
echo $config->get('version') . "\n";
echo ($config->get('missing') === null ? '0' : '1') . "\n";
Test yourself
-
Match the lazy loading concepts
- lazy loading
- Load on first access
- eager loading
- Load upfront
- lazy initialization
- Load when null, then cache
- deferred loading
- Deferred loading
-
When does lazy loading cause trouble?
- When fetching the relations of many items in a list
- When accessing an attribute of a single item
- When reading a config file once
- When triggering the garbage collector
Show answer
When fetching the relations of many items in a list — Correct. In list work lazy issues a query per item; that is N+1.
-
Complete the lazy get method
public function get(string $key): ?string { if ($this->data === ____) { $this->data = $this->load(); } return $this->data[$key] ?? null; }Show answer
public function get(string $key): ?string { if ($this->data === null) { $this->data = $this->load(); } return $this->data[$key] ?? null; }