Code前端首页关于Code前端联系我们

深入理解PHP中的Iterable接口

terry 2年前 (2023-09-29) 阅读数 69 #PHP
文章标签 PHP

1.什么是Iterable接口

Iterable接口是PHP 7.1中新添加的接口,用于指示类是否可迭代。只要类实现了 Iterator 或 IteratorAggregate 接口,就可以使用 foreach 对其进行迭代。

要实现Iterator接口,必须实现以下4个方法:

	class MyIterator implements Iterator {
	    private $items = [];
	    private $currentIndex = 0;
	
	    public function __construct(array $items) {
	        $this->items = $items;
	    }
	
	    public function rewind() {
	        $this->currentIndex = 0;
	    }
	
	    public function current() {
	        return $this->items[$this->currentIndex];
	    }
	
	    public function key() {
	        return $this->currentIndex;
	    }
	
	    public function next() {
	        $this->currentIndex++;
	    }
	
	    public function valid() {
	        return isset($this->items[$this->currentIndex]);
	    }
	}

要实现 IteratorAggregate 接口,必须实现 getIterator 方法,该方法返回实现 Iterator 接口的对象,例如:

	class MyIterable implements IteratorAggregate {
	    private $items = [];
	
	    public function __construct(array $items) {
	        $this->items = $items;
	    }
	
	    public function getIterator() {
	        return new MyIterator($this->items);
	    }
	}

2。函数可迭代接口

Iterable接口的功能是允许通过foreach语法遍历我们自定义的数据结构。

例如下面的例子,我们可以实现一个自定义的链表数据结构,并使用foreach语法来遍历链表:

	class ListNode {
	    public $val = 0;
	    public $next = null;
	
	    public function __construct($val) {
	        $this->val = $val;
	    }
	}
	
	class LinkedList implements IteratorAggregate {
	    public $head = null;
	    public $tail = null;
	    public $count = 0;
	
	    public function add($node) {
	        if ($this->head == null) {
	            $this->head = $node;
	            $this->tail = $node;
	        } else {
	            $this->tail->next = $node;
	            $this->tail = $node;
	        }
	        $this->count++;
	    }
	
	    public function getIterator() {
	        $node = $this->head;
	        while ($node) {
	            yield $node;
	            $node = $node->next;
	        }
	    }
	}
	
	$list = new LinkedList();
	$list->add(new ListNode(1));
	$list->add(new ListNode(2));
	$list->add(new ListNode(3));
	
	foreach ($list as $node) {
	    echo $node->val . " ";
	}
	// output: 1 2 3

通过实现 IteratorAggregate 接口并让 getIterator 方法返回生成器,您可以在自己的链表数据结构上实现 foreach 遍历。

3。可迭代接口的措施

如果在实现IteratorAggregate接口时,返回的迭代器没有实现Rewindable接口,则不会遍历key为0的元素。

例如:

	class MyIterator implements Iterator {
	    private $count = 3;
	    private $currentIndex = 0;
	
	    public function rewind() {
	        $this->currentIndex = 0;
	    }
	
	    public function current() {
	        return $this->currentIndex;
	    }
	
	    public function key() {
	        return $this->currentIndex;
	    }
	
	    public function next() {
	        $this->currentIndex++;
	        $this->count--;
	    }
	
	    public function valid() {
	        return $this->count > 0;
	    }
	}
	
	class MyIterable implements IteratorAggregate {
	    public function getIterator() {
	        return new MyIterator();
	    }
	}
	
	$iterable = new MyIterable();
	foreach ($iterable as $key => $value) {
	    echo "{$key} => {$value}\n";
	}
	// 输出为1 => 1, 2 => 2

在这个例子中,$key为0的元素不会被迭代,因为MyIterator没有实现Rewindable接口。如果您希望迭代所有元素,则需要在 MyIterator 中实现一个包装方法。

4。可迭代接口的扩展

除了 Iterator 和 IteratorAggregate 接口之外,PHP 还提供了 Generator 接口,它允许我们在调用生成器函数时使用 foreach 语法向生成器提供迭代器。同时Generator方法可以实现中断和恢复执行的能力,可以用于批量处理大量数据。

以数据切分为例,下面是处理csv文件时使用Generator实现文件切分处理的示例:

	function readData($filename, $blockSize) {
	    if (!$handle = fopen($filename, 'r')) {
	        throw new InvalidArgumentException("Cannot open file ($filename)");
	    }
	
	    $lineCount = 0;
	    $block = "";
	    while (!feof($handle)) {
	        $line = fgets($handle);
	        $block .= $line;
	        $lineCount++;
	        if ($lineCount >= $blockSize) {
	            $lineCount = 0;
	            yield $block;
	            $block = "";
	        }
	    }
	
	    if (!empty($block)) {
	        yield $block;
	    }
	    fclose($handle);
	}
	
	$filename = "large.csv";
	$blockSize = 1000;
	foreach (readData($filename, $blockSize) as $block) {
	    // process each block of data
	}
以下是在基于生成器的块中处理 csv 文件的示例。您可以使用 foreach 语法来提供生成器迭代器。它还支持生成器中断和恢复执行的能力,可用于处理大量数据进行分析。批量处理。

5。总结

Iterable 接口是 PHP 7.1 中的新接口。通过 Iterable 接口,您可以在使用 foreach 语法进行遍历时实现自定义数据结构。通过实现 Iterator 或 IteratorAggregate 接口并让 getIterator 方法返回可迭代对象,您可以在自定义数据结构上使用 foreach 语法。

除了 Iterator 和 IteratorAggregate 接口之外,PHP 还提供了 Generator 接口,可以在调用生成器函数时使用 foreach 语法向生成器提供迭代器。 Generator方法可以实现中断和恢复执行的能力,可以用来处理大量的数据块。

版权声明

本文仅代表作者观点,不代表Code前端网立场。
本文系作者Code前端网发表,如需转载,请注明页面地址。

热门