国产三级农村妇女在线,国产精品毛片a∨一区二区三区,国产乱子伦视频大全,国产精品色拉拉,国产欧美日韩一区二区三区,

首頁 > 技術(shù) > IOS開發(fā)

php聚合式迭代器的基礎(chǔ)知識(shí)點(diǎn)及實(shí)例代碼

IOS開發(fā) 2023-02-16 22:19:43

說明

1、實(shí)現(xiàn)其他迭代器功能的接口,相當(dāng)于在其他迭代器上安裝一個(gè)外殼,只有一種方法。

2、聚合迭代器可以與許多迭代器結(jié)合,實(shí)現(xiàn)更高效的迭代。

實(shí)例

class MainIterator implements Iterator
{
    private $var = array();
    public function __construct($array)    //構(gòu)造函數(shù), 初始化對(duì)象數(shù)組
    {
        if (is_array($array)) {
        $this->var = $array;
        }
    }
 
    public function rewind() {   
        echo "rewindingn";
        reset($this->var);    //將數(shù)組的內(nèi)部指針指向第一個(gè)單元
    }
 
    public function current() {
        $var = current($this->var);    // 返回?cái)?shù)組中的當(dāng)前值
        echo "current: $varn";
        return $var;
    }
 
    public function key() {
        $var = key($this->var);       //返回?cái)?shù)組中內(nèi)部指針指向的當(dāng)前單元的鍵名
        echo "key: $varn";
        return $var;
    }
 
    public function next() {
        $var = next($this->var);     //返回?cái)?shù)組內(nèi)部指針指向的下一個(gè)單元的值
        echo "next: $varn";
        return $var;
    }
 
    public function valid() {
    return !is_null(key($this->var); //判斷當(dāng)前單元的鍵是否為空
    }
}

內(nèi)容擴(kuò)展:

<?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";
}
?>

以上例程的輸出類似于:

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"

到此這篇關(guān)于php聚合式迭代器的基礎(chǔ)知識(shí)點(diǎn)及實(shí)例代碼的文章就介紹到這了,更多相關(guān)php聚合式迭代器是什么內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

TAg

加載中~

本網(wǎng)站LOGO受版權(quán)及商標(biāo)保護(hù),版權(quán)登記號(hào):國作登字-2022-F-10126915,未經(jīng)湖南木星科技官方許可,嚴(yán)禁使用。
Copyright ? 2012-2022 湖南木星科技有限公司(木星網(wǎng))版權(quán)所有
轉(zhuǎn)載內(nèi)容版權(quán)歸作者及來源網(wǎng)站所有,本站原創(chuàng)內(nèi)容轉(zhuǎn)載請(qǐng)注明來源,商業(yè)媒體及紙媒請(qǐng)先聯(lián)系:aishangyiwan@126.com

工信部備案號(hào):湘ICP備19012813號(hào)-5