Member-only story
Dependency Injection with PHP 8.4’s Lazy Objects
PHP 8.4 is out now for approximately 2 month and there are a lot of new features. One significant new feature is “Lazy Objects”, which is described as follows:
A lazy object is an object whose initialization is deferred until its state is observed or modified.
The past few weeks, I read a couple of blogs addressing Lazy Objects in the context of Dependency Injection and how it works together to boost up performance. I would like to discuss a few confusions and clarify some terminology.
This topic will also be published as a video on my YouTube channel: https://www.youtube.com/@doganoo
I will go into the topic in more detail there. So don’t forget to follow so you don’t miss anything!

Brief Recap: What is Dependency Injection
Dependency Injection (DI) is a design pattern that promotes loose coupling between components in software systems. Instead of a class creating its own dependencies, these are injected from an external source. This means a class simply declares what it needs, and another part of the application (e.g., a Dependency Injection container) provides those dependencies.
By separating the creation of dependencies from their usage, DI offers several advantages:
- Improved Testability: Dependencies can be easily mocked or replaced during unit testing.
- Flexibility: Dependencies can be swapped out or configured differently without modifying the class itself.
- Maintainability: Changes in dependency implementation don’t require changes in dependent classes.
There are several ways to implement DI:
- Constructor Injection: Dependencies are passed into a class via its constructor.
- Setter Injection: Dependencies are assigned using setter methods.
- Method Injection: Dependencies are passed directly to methods that require them.
Lazy Loading vs. Lazy Initialization: Similar Terms, Different Concepts
While lazy loading and lazy initialization are often mentioned together, they refer to distinct…