Php allows the object to perform a foreach loop like an array

Source: Internet
Author: User
Tags rewind

How can an object perform a foreach loop like an array?

I didn't take into account the Iterator mode when I first got into touch with the question. I tried some general ideas, and after the failure .... I directly looked at the source code implementation of foreach, hoping to find out whether there is any special feature when foreach processes objects. It can be used as a breakthrough.

After tracking for half a day, we found a strange switch in the core logic:

 
 
  1. switch (zend_iterator_unwrap(array, &iter TSRMLS_CC)) {
  2.         default:
  3.         case ZEND_ITER_INVALID:
  4.                .....
  5.                break
  6.         case ZEND_ITER_PLAIN_OBJECT: {
  7.                 ......
  8.             break;
  9.      case ZEND_ITER_PLAIN_ARRAY:
  10.             .....
  11.             break;
  12.         case ZEND_ITER_OBJECT:
  13.             ......
  14.             break;
  15. }

From this structure, we can see that the objects are divided into ZEND_ITER_OBJECT and ZEND_ITER_PLAIN_OBJECT. What does this mean?

 
 
  1. ZEND_API enum zend_object_iterator_kind zend_iterator_unwrap(
  2.     zval *array_ptr, zend_object_iterator **iter TSRMLS_DC)
  3. {
  4.     switch (Z_TYPE_P(array_ptr)) {
  5.         case IS_OBJECT:
  6.             if (Z_OBJ_HT_P(array_ptr) == &iterator_object_handlers) {
  7.                 *iter = (zend_object_iterator *)zend_object_store_get_object(array_ptr TSRMLS_CC);
  8.                 return ZEND_ITER_OBJECT;
  9.             }
  10.             if (HASH_OF(array_ptr)) {
  11.                 return ZEND_ITER_PLAIN_OBJECT;
  12.             }
  13.             return ZEND_ITER_INVALID;
  14.         case IS_ARRAY:
  15.             if (HASH_OF(array_ptr)) {
  16.                 return ZEND_ITER_PLAIN_ARRAY;
  17.             }
  18.             return ZEND_ITER_INVALID;
  19.         default:
  20.             return ZEND_ITER_INVALID;
  21.     }
  22. }

This is about PHP's built-in interface Iterator. PHP5 starts to support interfaces and has built-in Iterator interfaces. Therefore, 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 the class ZEND_ITER_PLAIN_OBJECT, foreach obtains the default attribute array of the object through HASH_OF, and then performs foreach on the array.
For the class object of ZEND_ITER_OBJECT, The foreach and Iterator interfaces are performed by calling the iterator Interface related functions implemented by the object:

IteratorExtends 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, you can give the following answers to this question:

 
 
  1. class sample implements Iterator
  2. {
  3.     private $_items = array(1,2,3,4,5,6,7);
  4.     public function __construct() {
  5.                   ;//void
  6.     }
  7.     public function rewind() { reset($this->_items); }
  8.     public function current() { return current($this->_items); }
  9.     public function key() { return key($this->_items); }
  10.     public function next() { return next($this->_items); }
  11.     public function valid() { return ( $this->current() !== false ); }
  12. }
  13. $sa = new sample();
  14. foreach($sa as $key => $val){
  15.     print $key . "=>" .$val;
  16. }

The above code runs normally in my php 5.3.

  • Address: http://www.laruence.com/2008/10/31/574.html
  • 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.