PHP-based Iterator pattern implementation

Source: Internet
Author: User
Tags spl rewind
This article mainly introduces the PHP based on the implementation of the iterator model, a simple description of the concept of the iterator pattern, the principle and analysis of PHP using SPL to implement the iterator mode of the relevant operating skills and considerations, the need for friends can refer to the following

The example in this paper describes the iterator pattern implemented by PHP based on SPL. Share to everyone for your reference, as follows:

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);

This article refers to the in-depth understanding of PHP advanced techniques, object-oriented and core technologies

Related Article

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.