PHP Iterator Interface detailed

Source: Internet
Author: User
Tags php class rewind

As we all know, arrays can be done by using a Foreach loop, and we can also use an object as an array to do the loop operation.

1. Object Inheritance Iterator interface

<?php class Myiterator implements iterator{private $_d = Array (' A ', ' B ', ' C ', ' d ');
			Private $_p = 0;
			Public Function __construct () {$this->_p = 0; 
				/** * Returns the current value * * @see iterator::current () * * * ()/Public function () {var_dump (__method__);
				
			return $this->_d[$this->_p]; /** * Current Index plus 1 * * * @see Iterator::next ()/Public function next () {Var_dump (__metho
				D__);
			$this->_p++; /** * Returns the current index value * * * @see Iterator::key ()/Public Function key () {Var_dump (__method__)
				;
				
			return $this->_p;
				/** * Verify the current value * * @see iterator::valid ()/Public function valid () {var_dump (__method__);
				
			return Isset ($this->_d[$this->_p]); /** * Index Array to 0 * * * @see iterator::rewind ()/Public Function rewind () {Var_dump (__me
				THOD__);
			$this->_p = 0; }} $me = new myiTerator (); foreach ($me as $key => $value) {var_dump ($key). ':' .
	Var_dump ($value); The results of   are shown below:

Let's analyze:

1. Set the index of the array in the object to 0.

2. Verify that there is a value under the index.

3, if there is, return value.

4, return index value.

5, call next function, index value plus 1.

6, repeat 2, 3, 4, step playing. If you verify that there is no value under an index, break it.

Disadvantages: 1, we need to implement so many functions (5).

2, said above, if there is no value under an index, then interrupted, not in the loop, if our array is such a private $_d = Array (' A ', ' B ', ' C ', ' d ', 8 => ' F ');. Then the F value will not be output.

Let's solve the first shortcoming first.

Does not implement the interface iterator, realizes his sub-interface iteratoraggregate

Class MyData implements Iteratoraggregate {public
		    $property 1 = ' public property one ';
		    Public $property 2 = "two";
		    Public $property 3 = "three";
		
		    Public Function __construct () {
		        $this->property4 = ' last property ';
		    }
		
		    Public Function Getiterator () {return
		        new Arrayiterator ($this);
		    }
		}
		
		$obj = new MyData;
		
		foreach ($obj as $key => $value) {
		    var_dump ($key, $value);
		    echo "\ n";
		}

The output is shown below:

We just have to implement a method Getiterator OK. It's not easy. We can also directly achieve myiterator, to see what kind of results, to try their own.

To solve the second disadvantage, this requires us to improve the myiterator to ensure that when the index is not continuous, you can output the following values. Encapsulation of the idea ah, let's try it.

Let's say the Traversable interface, what kind of sampling method is not in this interface, he is the iterator of the parent interface. At the bottom, this interface is generally not going to implement it, and if you have to implement it, remember two points.

1, must and iterator, iteratoraggregate two interface with one or two.

2, iteratoraggregate or iterator must be placed in front of traversable. such as: Class Myiterator implements iterator,traversable{}. Remember.

The above we are talking about the loop-related interface, we will say that the value of the assignment, destroy the value of the interface, which is arrayaccess.

First, a piece of code that implements the following interface

<?php class MyArray implements arrayaccess{private $_d = Array (' A ', ' V ', ' B ', ' d '); /** * Detects if a value under an index exists * * @see arrayaccess::offsetexists ()/Public function offsetexists ($offset) {var
			_dump (__method__);
			
		return $this->_d[$offset]!== null; /** * Obtains a value on an index * * @see arrayaccess::offsetget ()/Public function Offsetget ($offset) {Var_dump
			(__method__);
			
		return $this->_d[$offset];
			/** * Sets the value on an index * * @see arrayaccess::offsetset ()/Public function Offsetset ($offset, $value) {
			Var_dump (__method__);
			
		$this->_d[$offset] = $value;
			/** * Destroy value at an index * * * @see Arrayaccess::offsetunset ()/Public function Offsetunset ($offset) {
			Var_dump (__method__);
		$this->_d[$offset] = null;
		}} $me = new MyArray ();
		echo $me [0];
		echo "<br/>";
		$me [5] = ' t ';
		echo "<br/>";
		Empty ($me [1]);
		echo "<br/>";
		 Unset ($me [2]);
The result is as shown in figure:

A little problem, left to everyone:

If we want to test empty ($me [10]), what will happen, let's try it out.

The implementation of these interfaces is entirely for the encapsulation, so that it better serve us.

Finally, it is only a personal idea, we share the learning. Thank you





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.