Basic knowledge points and example code of PHP aggregate iterator

Time:2022-5-8

explain

1. The interface to realize the functions of other iterators is equivalent to installing a shell on other iterators. There is only one method.

2. Aggregate iterators can be combined with many iterators to achieve more efficient iterations.

example

class MainIterator implements Iterator
{
    private $var = array();
    public function __ Construct ($array) // constructor to initialize the object array
    {
        if (is_array($array)) {
        $this->var = $array;
        }
    }
 
    public function rewind() {   
        echo "rewinding\n";
        reset($this->var);    // Point the internal pointer of the array to the first cell
    }
 
    public function current() {
        $var = current($this->var);    //  Returns the current value in the array
        echo "current: $var\n";
        return $var;
    }
 
    public function key() {
        $var = key($this->var);       // Returns the key name of the current cell pointed to by the internal pointer in the array
        echo "key: $var\n";
        return $var;
    }
 
    public function next() {
        $var = next($this->var);     // Returns the value of the next cell pointed to by the pointer inside the array
        echo "next: $var\n";
        return $var;
    }
 
    public function valid() {
    return ! is_ null(key($this->var); // Judge whether the key of the current cell is empty
    }
}

Content extension:


<?php
class myData implements IteratorAggregate {
    public $property1 = "Public property one";
    public $property2 = "Public property two";
    public $property3 = "Public property 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 of the above routine is similar to:

string(9) “property1”
string(19) “Public property one”

string(9) “property2”
string(19) “Public property two”

string(9) “property3”
string(21) “Public property three”

string(9) “property4”
string(13) “last property”

This is the end of this article about the basic knowledge points and example code of PHP aggregate iterator. For more information about what PHP aggregate iterator is, please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!

Recommended Today

CSC415 Command Line Arguments

CSC415-Assignment-1Assignment 1 – Command Line Arguments Welcome to your First homework assignment.Let me set some of the expectations. Code should be neat, well documented. Variables should have meaningful names and be in a consistent format (I do not care if you use camelCase or under_scores in variables but be consistent.All filenames should be <lastname><firstname>_HW<#><optional>.<proper extension>, except […]