Lazy Loop: How Generators Saved My PHP Applications

jochelle mendonca
3 min readFeb 29, 2024

--

Have you ever had a situation where your PHP application’s performance suffered because it took so long to load a large dataset?

I definitely have been there.

When I first started out, I mainly relied on arrays for my enormous datasets and numerous retrieval queries. But as soon as my code was put through reviews, it came to an abrupt end. And it was here that my journey took a drastic turn when my senior introduced me to the realm of generators.

So, Introducing generators, an indispensable PHP feature that makes handling data more effective and memory-friendly.

Photo by Susan Holt Simpson on Unsplash

Overview of Generators: What Are They?

Imagine a water fountain. Instead of filling a giant bucket all at once, you get water one cup at a time. In PHP generators, work similarly.

They are dynamic constructions that generate values on-demand, instead of loading it all into memory at once — to get over the limitations of arrays.

This is called Lazy Loading

Generators are different from arrays in that they prioritize efficiency and memory conservation over loading all values into memory at once.

function generateNumbers($max) {
for ($i = 1; $i <= $max; $i++) {
yield $i;
}
}

foreach (generateNumbers(1000000) as $number) {
echo $number . PHP_EOL;
}

Benefits:

  1. Efficiency or Lazy Evaluation: Generators take a “lazy” approach to evaluation, producing values solely in response to requests. This makes it possible to use memory effectively, particularly when working with datasets that are unknown or possibly limitless in size.
  2. Infinite Sequences: With generators, you can effortlessly create infinite sequences of values, such as an infinite list of prime numbers or a sequence of Fibonacci numbers, without worrying about memory constraints.
  3. Stream Processing: Generators facilitate seamless stream processing, enabling you to process data in a sequential manner without loading the entire dataset into memory at once. This is particularly useful when dealing with streaming data sources or large files, thus delivering a noticeable performance boost.
  4. Code Readability: By dividing complicated data retrieval logic into smaller, easier-to-manage phases, they can make it simpler.

Cons:

  1. Complexity: This holds true, when I first forayed into implementing and learning about generators it proved less intuitive than traditional arrays with their concepts and usage.
  2. Limited Random Access: Unlike arrays, generators do not support random access, limiting their applicability in scenarios requiring direct access to elements by index.

Tips and Tricks:

  1. To return values, use yield: Consider yield as the “cup” that distributes information piece by piece.
  2. Generators of chains: To generate complex data flows, combine numerous generators.
  3. Employing the foreach loop: Like any other array, a generator can be looped through, but it will only produce data as you go through it.

Conclusion

To sum up, PHP generators are an effective tool for handling data efficiently, particularly when dealing with big datasets or streaming data sources and understanding it, will unlock a new level of efficiency and memory management in your PHP applications.

Remember, sometimes the best solutions come from a different perspective, lazy in this case, but incredibly efficient!

If you liked this article and would like to support me, make sure to:

  • 👏Clap for this story
  • 🔔Follow me on Medium
  • ☕️ If you feel my work is worth an appreciation, you can buy me a coffee!

--

--

jochelle mendonca

Passionate PHP developer. Enthusiastic about the power of words, equally adept at reading and writing