Home  >  Article  >  Backend Development  >  What is a php generator? 10 minutes to help you understand PHP generator with examples

What is a php generator? 10 minutes to help you understand PHP generator with examples

墨辰丷
墨辰丷Original
2018-06-07 15:37:142855browse

Before explaining the generator, let’s introduce iterators:

In PHP, usually you use foreach to traverse an array.

What if we want to make an object traversable?

PHP provides us with the Iterator interface. As long as this interface is implemented, this object can be iterated through foreach.

The example is as follows:

What is a php generator? 10 minutes to help you understand PHP generator with examples

When we traverse $it through foreach, PHP will call itself in sequence:

rewind() reset to The first element

valid() checks whether the current position is valid

current() returns the current element

key() returns the key of the current element

next() points to the next element

The generator is a new feature introduced in PHP 5.5, but few people seem to use it currently.

What is a php generator?

The following is the explanation of the generator from the official PHP documentation:

The generator provides an easier way to implement simple object iteration, compared to defining a class to implement Iterator Interface approach, performance overhead and complexity are greatly reduced.

Generators allow you to write code in a foreach block to iterate over a set of data without creating an array in memory, which would hit your memory limit or take up considerable processing time. Instead, you can write a generator function, just like a normal custom function, and instead of a normal function returning only once, the generator can yield as many times as needed to generate the values ​​that need to be iterated over.

In order to reflect the advantages of the generator, below we define a function for comparison:

What is a php generator? 10 minutes to help you understand PHP generator with examples

Because creating such a large array into memory for iteration, then PHP directly prompts that the memory limit of a single process has been exceeded.

Let’s switch to the generator method:

What is a php generator? 10 minutes to help you understand PHP generator with examples

You can see that we call func1() and return a Generator object, which can be used foreach iteration, each iteration, PHP will ask the Generator instance to calculate and provide the next value to be iterated. The elegance of generators is that each time a value is produced, the internal state of the generator is paused; when the next value is requested from the generator, the internal state is restored. The internal state of the generator will continue to switch between pause and resume until the end of the function definition body is reached or an empty return statement is encountered.

The above is the detailed content of What is a php generator? 10 minutes to help you understand PHP generator with examples. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn