您好,登錄后才能下訂單哦!
介紹
可在內(nèi)部迭代自己的外部迭代器或類的接口。
Iterator extends Traversable { /* 方法 */ abstract public mixed current(void) abstract public scalar key(void) abstract public void next(void) abstract public void rewind(void ) abstract public boolean valid(void) }
舉個(gè)栗子,定義一個(gè)迭代器:
class myIerator implements Iterator { protected $position;//指針 public $arr = []; public function __construct(array $arr) { $info = date('Y-m-d H:i:s') . ' ' . __METHOD__ . ' Line :' . __LINE__ . "\r\n"; $this->writeLog($info); $this->arr = $arr; return $this; } //獲取當(dāng)前的值 public function current() { $info = date('Y-m-d H:i:s') . ' ' . __METHOD__ . ' Line :' . __LINE__ . "\r\n"; $this->writeLog($info); return $this->arr[$this->position] ?? null; } //將指針移至下一位 public function next() { $info = date('Y-m-d H:i:s') . ' ' . __METHOD__ . ' Line :' . __LINE__ . "\r\n"; $this->writeLog($info); return ++$this->position; } //返回當(dāng)前的指針 public function key() { $info = date('Y-m-d H:i:s') . ' ' . __METHOD__ . ' Line :' . __LINE__ . "\r\n"; $this->writeLog($info); return $this->position; } //重置指針 public function rewind() { $info = date('Y-m-d H:i:s') . ' ' . __METHOD__ . ' Line :' . __LINE__ . "\r\n"; $this->writeLog($info); $this->position = 0; } //檢查當(dāng)前是否有效 public function valid() { $info = date('Y-m-d H:i:s') . ' ' . __METHOD__ . ' Line :' . __LINE__ . "\r\n"; $this->writeLog($info); return isset($this->arr[$this->position]); } private function writeLog(string $info) { error_log($info, 3, './debug.log'); } }
$zhangsan=['one','two','three','four']; $arr = new myIerator($zhangsan); foreach ($arr as $k => $v) { echo "{$k} => {$v} \r\n"; }
打印結(jié)果:
0 => one
1 => two
2 => three
3 => four
查看調(diào)用日志:
2017-11-27 15:56:04 myIerator::__construct Line :16
2017-11-27 15:56:04 myIerator::rewind Line :50 重置指針
2017-11-27 15:56:04 myIerator::valid Line :58 校驗(yàn)是否有效
2017-11-27 15:56:04 myIerator::current Line :25 獲取當(dāng)前指針的值
2017-11-27 15:56:04 myIerator::key Line :41 獲取當(dāng)前指針位置
2017-11-27 15:56:04 myIerator::next Line :33 指針下移一位(進(jìn)入下一個(gè)循環(huán))
2017-11-27 15:56:04 myIerator::valid Line :58
2017-11-27 15:56:04 myIerator::current Line :25
2017-11-27 15:56:04 myIerator::key Line :41
2017-11-27 15:56:04 myIerator::next Line :33
2017-11-27 15:56:04 myIerator::valid Line :58
2017-11-27 15:56:04 myIerator::current Line :25
2017-11-27 15:56:04 myIerator::key Line :41
2017-11-27 15:56:04 myIerator::next Line :33
2017-11-27 15:56:04 myIerator::valid Line :58
2017-11-27 15:56:04 myIerator::current Line :25
2017-11-27 15:56:04 myIerator::key Line :41
2017-11-27 15:56:04 myIerator::next Line :33
2017-11-27 15:56:04 myIerator::valid Line :58
//遍歷方式二:
$arr->rewind();
while ($arr->valid()) {
$key = $arr->key();
$value = $arr->current();
echo "{$key} => {$value} \r\n";
$arr->next();
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。