How to make an object a foreach loop like an array in PHP _php tutorial

Source: Internet
Author: User
Tags rewind
Just touched the question, I did not consider the iterator model, tried a few general ideas, after the failure .... Just go to look at the source of the foreach implementation, expect to find out when the foreach processing object when there is any particularity, can be done as a breach.

After a half-day of tracking, a strange switch in the core logic was found:
Copy CodeThe code is as follows:
Switch (zend_iterator_unwrap (array, &iter tsrmls_cc)) {
Default
Case Zend_iter_invalid:
.....
Break
Case Zend_iter_plain_object: {
......
Break
Case Zend_iter_plain_array:
.....
Break
Case Zend_iter_object:
......
Break
}

From this structure, we can see that the objects are divided into Zend_iter_object and zend_iter_plain_object, what does that mean?
Copy CodeThe code is as follows:
Zend_api enum Zend_object_iterator_kind Zend_iterator_unwrap (
Zval *array_ptr, Zend_object_iterator **iter tsrmls_dc)
{
Switch (z_type_p (array_ptr)) {
Case Is_object:
if (z_obj_ht_p (array_ptr) = = &iterator_object_handlers) {
*iter = (Zend_object_iterator *) zend_object_store_get_object (array_ptr tsrmls_cc);
return zend_iter_object;
}
if (hash_of (array_ptr)) {
return zend_iter_plain_object;
}
return zend_iter_invalid;
Case Is_array:
if (hash_of (array_ptr)) {
return zend_iter_plain_array;
}
return zend_iter_invalid;
Default
return zend_iter_invalid;
}
}

This is about PHP built-in interface iterator, PHP5 began to support the interface, and built-in iterator interface, so if you define a class, and implement the iterator interface, then your class object is Zend_iter_object, Otherwise it is zend_iter_plain_object.

For Zend_iter_plain_object classes, foreach obtains an array of default properties for the object through Hash_of, and then foreach the array.

For Zend_iter_object class objects, the Foreach,iterator interface is performed by invoking the Iterator interface correlation function implemented by the object:
Copy CodeThe code is as follows:
Iterator extends Traversable {
/* Method */
Abstract public mixed current (void)
Abstract public scalar key (void)
Abstract public void next (void)
Abstract public void rewind (void)
Abstract public boolean valid (void)
}

Therefore, for this question, the following answers can be made:
Copy CodeThe code is as follows:
Class Sample Implements Iterator
{
Private $_items = Array (1,2,3,4,5,6,7);
Public Function __construct () {
;//void
}
Public Function Rewind () {Reset ($this->_items);}
Public function current () {return current ($this->_items);}
Public Function key () {return key ($this->_items);}
Public function Next () {return next ($this->_items);}
Public Function valid () {return ($this->current ()!== false);}
}
$sa = new sample ();
foreach ($sa as $key = = $val) {
Print $key. "=". $val;
}

The above code runs normally under my PHP 5.3.

http://www.bkjia.com/PHPjc/328120.html www.bkjia.com true http://www.bkjia.com/PHPjc/328120.html techarticle just touched the question, I did not consider the iterator model, tried a few general ideas, after the failure .... Just go to look at the source of foreach, expect to find Foreac ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.