PHP-based iterator implementation steps in detail

Source: Internet
Author: User
Tags spl rewind
This time to bring you a PHP-based implementation of the iterator steps in detail, PHP based on the implementation of a SPL iterator considerations, the following is the actual case, together to see.

Now there are two classes, Department department, employee employees category:

Department class department{  private $_name;  Private $_employees;  function construct ($name) {    $this->_name = $name;    $this->employees = Array ();  }  function AddEmployee (Employee $e) {    $this->_employees[] = $e;    echo "Employee {$e->getname ()} is assigned to {$this->_name} to go";}  } Employee class employee{  private $_name;  function construct ($name) {    $this->_name = $name;  }  function GetName () {    return $this->_name;  }} Application: $lsgo = new Department (' Lsgo lab '), $e 1 = new Employee ("Xiao Jin"), $e 2 = new Employee ("Piglet"), $lsgo->addemployee ($e 1); Lsgo->addemployee ($e 2);

Well, now that there are two departments in the Lsgo lab, I want to list all the departments now, which is to use loops to get the details of each employee in the department.

Here we implement the iterator provided by the SPL standard library in PHP.

"Big Talk design mode" In this way:

Iterator mode: The iterator pattern is the mature mode of traversing the collection, the key to the iterator pattern is to pass the task of iterating over the collection to an object called an iterator, which traverses and selects the objects in the sequence when it works, and the client programmer does not have to know or care about the underlying structure of the collection sequence.

The role of the iterator pattern is simply that the components of all complex data structures can use loops to access

If our object is to iterate, we make this class implement Iterator (the SPL standard library provides), which is an iterator interface, in order to implement the interface, we must implement the following methods:

current(), the function returns the current data item
key()That returns the key of the current data item or the position of the item in the list
next()That enables the key or position of the data item to move forward
rewind(), the function resets the key value or position
valid(), the function returns a bool value indicating whether the current key or position points to a data value

After implementing the Iterator interface and the prescribed method, PHP is able to know that objects of that class type need to be iterated.

We use this method to reconstruct the Department class:

class Department implements iterator{private $_name;  Private $_employees;    Private $_position;//flag Current array pointer position function construct ($name) {$this->_name = $name;    $this->employees = Array ();  $this->_position = 0;    } function AddEmployee (Employee $e) {$this->_employees[] = $e;  echo "Employee {$e->getname ()} is assigned to {$this->_name}";  }//Implement the Iterator interface requires the implementation of the method function current () {return $this->_employees[$this->_position];  } function key () {return $this->_position;  } function Next () {$this->_position++;  } function Rewind () {$this->_position = 0;  } function valid () {return isset ($this->_employees[$this->_position]); }}//employee class with previous//applications: $lsgo = new Department (' Lsgo lab '); $e 1 = new Employee ("Xiao Jin"); $e 2 = new Employee ("Piglet"); $lsgo AddEmployee ($e 1), $lsgo->addemployee ($e 2), echo "Lsgo Laboratory staff Situation:";//The $_employeeforeach ($lsgo as $val) {echo "section here actually traversed) {$val->getname ()} ";} 

Additional:

If we want to know that the department has a few employees, if it is an array, a count() function is OK, then can we treat the object as an array as above? The countable interface is available in the SPL standard library for our use:

Class Department implements iterator,countable{  //Previous Ibid  //implementation countable required method  function count () {    Return count ($this->_employees);}  } Application: echo "Number of employees:"; echo count ($lsgo);

Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!

Recommended reading:

PHP7 Operation MongoDB Additions and deletions to investigate the steps

PHP Factory Method Design Model case study

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.