Iterator iterator in PHP (traversal)

Source: Internet
Author: User

Traversing an operation that is primarily used to traverse an element, as with every element in an array, we can use iterators


Interface demo//here describes the interfaces that are provided to developers
{
Public function fn1 ();
Public function fn2 ();
Public function fn3 ();
}


Interface Demo2 extends demo//This step is to hide the specific implementation of the interface
{
Public function fn4 ();
}


Class Demo3 implements demo//users can call these methods on their own to implement the processing of these objects
{
Public Function Fn4 ()
{
echo ' function 2 ';
}
Public Function fn1 () {}
Public Function fn2 () {}
Public Function Fn3 () {}
}
$demo 3 = new Demo3 ();
$demo 3->fn4 ()



My understanding is that the program provides the interface, and we can define the algorithm to achieve it, so some people say it can improve efficiency.



The above understanding may not be full, I put the understanding of Daniel's advice, in fact, is also a simple thing








<span style= "Font-size:18px;color: #cc0000;" ><?php


class Myiterator implements iterator
{public
	$position = 0;
	Public $arr = Array (' One ', ' two ', ' three ', ' four ', ' five ', ' six ');

	Public function __construct ()
	{
		$this->position = 0;
	}

	Public Function Rewind ()
	{
		var_dump (__method__);
		$this->position = 0;
	}

	Public function current ()
	{
		var_dump (__method__);
		return $this->arr[$this->position];

	Public Function key ()
	{
		var_dump (__method__);
		return $this->position;
	}

	Public function Next ()
	{
		var_dump (__method__);
		+ + $this->position;
	}
	Public function valid ()
	{
		var_dump (__method__);
		return Isset ($this->arr[$this->position]);
	}

$it = new Myiterator;
foreach ($it as $key => $value)
{
	var_dump ($key, $value);
	echo "\ n";
} </span>

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.