A brief discussion on PHP source code twenty-four: Analysis of the reasons why iteration cannot be completed when the value is false in iterator implementation

不言
Release: 2023-04-02 06:08:01
Original
1323 people have browsed it

This article mainly introduces the analysis of the reasons why iteration cannot be completed when the value is false in the iterator implementation. It is now shared with everyone. Friends in need can refer to it. Let’s talk about PHP source code twenty-four: Analysis of the reasons why iteration cannot be completed when the value is false in iterator implementation

There is an article in the previous article about the simple implementation of iterator and the Yii framework There is a simple iterator implementation in the iterator implementation. There is a problem left here. When the iteration value contains false, the foreach loop ends at this place. What is the reason?

Follow Brother Bird’s idea and find relevant clues at line 8131 of the Zend/zend_vm_execute.h file, as shown below:

 /*  */
 if (!iter || (iter->index > 0 && iter->funcs->valid(iter TSRMLS_CC) == FAILURE)) {
 /* reached end of iteration */
 if (EG(exception)) {
 array->refcount--;zval_ptr_dtor(&array);ZEND_VM_NEXT_OPCODE();}
 ZEND_VM_JMP(EX(op_array)->opcodes+opline->op2.u.opline_num);}
Copy after login

For the simple iterator implemented, iter->funcs- >valid(iter TSRMLS_CC) method calls the valid() method,

If our value is false, the value returned through current is false, and when accessed through foreach, the traversal is interrupted here. , the program will continue to execute the following code instead of this loop

Solution

Process the key and value in the array separately

In the valid(), rewind(), next() methods Operate key in instead of value
Only return value in current
Such as the simple implementation of the iterator in the article and the implementation of the iterator in the Yii framework and the implementation of CMapIterator in the Yii framework

Above That’s the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

A brief discussion on PHP source code twenty-three: About the urlencode function


A brief discussion on PHP source code twenty-three 2: About array_splice function

The above is the detailed content of A brief discussion on PHP source code twenty-four: Analysis of the reasons why iteration cannot be completed when the value is false in iterator implementation. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template