Home  >  Article  >  Backend Development  >  Analysis of iterators and generators in PHP and introduction to their advantages and disadvantages

Analysis of iterators and generators in PHP and introduction to their advantages and disadvantages

不言
不言Original
2018-08-11 16:57:562490browse

What this article brings to you is the analysis and introduction of the advantages and disadvantages of iterators and generators in PHP. It has certain reference value. Friends in need can refer to it. I hope It will help you.

1. Iterator

Analysis: Think about it, if you put the collection object and the operation on the collection object Together, when we want to traverse the elements in the collection object in another way, we need to modify the collection object, which violates the "single responsibility principle", and the iterator pattern separates the data structure and the algorithm of the data structure, and the two can be independent develop.

Advantages:

1. Supports multiple traversal methods. For example, in an ordered list, we provide two iterators for forward traversal and reverse order traversal as needed. Users only need to get our iterator to perform traversal operations on the collection

2. Simplified aggregate classes. Due to the introduction of iterators, the original collection object does not need to traverse the collection elements by itself

3. It is very convenient to add new aggregation classes and iterator classes, and the two dimensions can be changed independently

4. Provide a unified interface for different collection structures, thereby supporting the same algorithm to operate on different collection structures

Disadvantages:

1. Iteration The iterator pattern separates the responsibilities of storing data and traversing data. When adding a new collection object, you need to add a corresponding iterator class. The number of classes increases in pairs, which increases the system complexity to a certain extent.

Specific interface:

Iterator extends Traversable {
/* 方法 */
abstract public mixed current ( void )
abstract public scalar key ( void )
abstract public void next ( void )
abstract public void rewind ( void )
abstract public bool valid ( void )
}

2. Generator

PHP generator (generator) is a function introduced in PHP5.5.0. Unlike standard PHP iterators, PHP generators do not require The class implements the Iterator interface, thus reducing the burden on the class. The generator will calculate and output the values ​​to be iterated according to requirements. This has a significant impact on performance. Imagine if the standard PHP iterator often performs iteration operations in memory. Pre-computing a data set has low performance; if a specific method is used to calculate a large amount of data, the impact on performance is even greater. At this time, we can use the generator to instantly calculate and output subsequent values ​​without occupying valuable memory resources.

Advantages: takes up less memory and has good performance. The generator's internal state is paused each time it produces a value; it is restored when the generator requests the next value. The internals of the generator keep switching between pause and resume until the loop is completed or the pause position

Disadvantages:

1. The PHP generator cannot satisfy the needs of all iterators, because if it is not queried , the generator never knows what the next value to iterate over is, there is no way to go back or forward in the generator.

2. The generator is still one-time, and the same generator cannot be iterated multiple times. However, if necessary, the generator can be rebuilt or cloned.

Create a generator:

1. Because the generator is a PHP function, the generator uses the yield keyword in the function. Unlike ordinary PHP functions, producers never return values, they only produce values.

2. When calling the generator function, PHP will return an object belonging to the Generator class. This object can be iterated using the foreach() function. For each iteration, PHP will ask the Generator instance to calculate and provide the next value to be iterated

As shown above: When $length is large (millions), and you are not using a generator at the same time, Then you need to pre-allocate memory for an array consisting of one million to one thousand five integers. The PHP generator can achieve the same operation, but it will only allocate memory for one integer at a time

Related recommendations:

php uses bit operations to implement addition, subtraction, multiplication and division of integers and test ( Code example)

Code for simple interaction between PHP and html form

The above is the detailed content of Analysis of iterators and generators in PHP and introduction to their advantages and disadvantages. 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