PHP Iterator Pattern Example Based on SPL Implementation

  • 2021-09-24 21:45:48
  • OfStack

This paper illustrates the iterator pattern of PHP based on SPL. Share it for your reference, as follows:

There are now two classes, the Department department class and the Employee employee class:


// Sectoral category 
class Department{
  private $_name;
  private $_employees;
  function __construct($name){
    $this->_name = $name;
    $this->employees = array();
  }
  function addEmployee(Employee $e){
    $this->_employees[] = $e;
    echo " Employees {$e->getName()} Be assigned to {$this->_name} Zhongqu ";
  }
}
// Employee category 
class Employee{
  private $_name;
  function __construct($name){
    $this->_name = $name;
  }
  function getName(){
    return $this->_name;
  }
}
// Application: 
$lsgo = new Department('LSGO Laboratory ');
$e1 = new Employee(" Small brocade ");
$e2 = new Employee(" Piglet ");
$lsgo->addEmployee($e1);
$lsgo->addEmployee($e2);

Ok, now that LSGO lab has two department members, I want to list all the department members, which is to use a loop to get the details of each employee in the department.

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

It is said in "Big Talk Design Pattern":

Iterator pattern: Iterator pattern is a mature pattern for traversing collections. The key of iterator pattern is to hand over the task of traversing collections to an object called iterator, which traverses and selects objects in a sequence when working, while client programmers do not need to know or care about the underlying structure of the collection sequence.

The function of the iterator pattern is, in a nutshell, to make all components of a complex data structure accessible using loops

If our object is going to implement iteration, we make this class implement Iterator (provided by the SPL standard library), which is an iterator interface. To implement this interface, we must implement the following methods:

current() This function returns the current data item
key() This function returns the key of the current data item or the position of the item in the list
next() This function moves the key or position of the data item forward
rewind() This function resets the key value or position
valid() Which returns an bool value indicating whether the current key or position points to a data value

After implementing the Iterator interface and the specified methods, PHP knows that objects of this type need to be iterated.

We reconstruct the Department class in this way:


class Department implements Iterator
{
  private $_name;
  private $_employees;
  private $_position;// Flags the current array pointer position 
  function __construct($name)
  {
    $this->_name = $name;
    $this->employees = array();
    $this->_position = 0;
  }
  function addEmployee(Employee $e)
  {
    $this->_employees[] = $e;
    echo " Employees {$e->getName()} Be assigned to {$this->_name} Zhongqu ";
  }
  // Realization  Iterator  The method required to be implemented by the interface 
  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  Before similarity 
// Application: 
$lsgo = new Department('LSGO Laboratory ');
$e1 = new Employee(" Small brocade ");
$e2 = new Employee(" Piglet ");
$lsgo->addEmployee($e1);
$lsgo->addEmployee($e2);
echo "LSGO Laboratory staff: ";
// In fact, traversing here $_employee
foreach($lsgo as $val){
  echo " Department member {$val->getName()}";
}

Additional:

If we want to know how many employees there are in this department now, if it is an array, 1 count() The function is ok, so can we treat objects as arrays like above? The Countable interface is provided in the SPL standard library for our use:


class Department implements Iterator,Countable{
  // Same as above 
  // Realization Countable Methods required to be implemented in 
  function count(){
    return count($this->_employees);
  }
}
// Application: 
echo " Number of employees: ";
echo count($lsgo);

This article is referenced from "In-depth understanding of PHP advanced skills, object-oriented and core technologies"

For more readers interested in PHP related content, please check the topics on this site: "Introduction to php Object-Oriented Programming", "Encyclopedia of PHP Array (Array) Operation Skills", "Introduction to PHP Basic Grammar", "Summary of PHP Operation and Operator Usage", "Summary of php String (string) Usage", "Introduction to php+mysql Database Operation" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: