PHP的类与对象中文手册:遍历对象
2019-09-07 阅读 : 次
PHP 5 提供了一种定义对象的方法使其可以通过单元列表来遍历,例如用 foreach 语句。默认情况下,所有可见属性都将被用于遍历。
Example #1 简单的对象遍历
<?php
class MyClass
{
public $var1 = 'value 1';
public $var2 = 'value 2';
public $var3 = 'value 3';
protected $protected = 'protected var';
private $private = 'private var';
function iterateVisible() {
echo "MyClass::iterateVisible:/n";
foreach($this as $key => $value) {
print "$key => $value/n";
}
}
}
$class = new MyClass();
foreach($class as $key => $value) {
print "$key => $value/n";
}
echo "/n";
$class->iterateVisible();
?>
以上例程会输出:
var1 => value 1var2 => value 2var3 => value 3MyClass::iterateVisible:var1 => value 1var2 => value 2var3 => value 3protected => protected varprivate => private var
如上所示,foreach 遍历了所有其能够访问的可见属性。
更进一步,可以实现 Iterator 接口。可以让对象自行决定如何遍历以及每次遍历时那些值可用。
Example #2 实现 Iterator 接口的对象遍历
<?php
class MyIterator implements Iterator
{
private $var = array();
public function __construct($array)
{
if (is_array($array)) {
$this->var = $array;
}
}
public function rewind() {
echo "rewinding/n";
reset($this->var);
}
public function current() {
$var = current($this->var);
echo "current: $var/n";
return $var;
}
public function key() {
$var = key($this->var);
echo "key: $var/n";
return $var;
}
public function next() {
$var = next($this->var);
echo "next: $var/n";
return $var;
}
public function valid() {
$var = $this->current() !== false;
echo "valid: {$var}/n";
return $var;
}
}
$values = array(1,2,3);
$it = new MyIterator($values);
foreach ($it as $a => $b) {
print "$a: $b/n";
}
?>
以上例程会输出:
rewindingcurrent: 1valid: 1current: 1key: 00: 1next: 2current: 2valid: 1current: 2key: 11: 2next: 3current: 3valid: 1current: 3key: 22: 3next:current:valid:
可以用 IteratorAggregate 接口以替代实现所有的 Iterator 方法。IteratorAggregate 只需要实现一个方法 IteratorAggregate::getIterator(),其应返回一个实现了 Iterator 的类的实例。
Example #3 通过实现 IteratorAggregate 来遍历对象
<?php
class MyCollection implements IteratorAggregate
{
private $items = array();
private $count = 0;
// Required definition of interface IteratorAggregate
public function getIterator() {
return new MyIterator($this->items);
}
public function add($value) {
$this->items[$this->count++] = $value;
}
}
$coll = new MyCollection();
$coll->add('value 1');
$coll->add('value 2');
$coll->add('value 3');
foreach ($coll as $key => $val) {
echo "key/value: [$key -> $val]/n/n";
}
?>
以上例程会输出:
rewindingcurrent: value 1valid: 1current: value 1key: 0key/value: [0 -> value 1]next: value 2current: value 2valid: 1current: value 2key: 1key/value: [1 -> value 2]next: value 3current: value 3valid: 1current: value 3key: 2key/value: [2 -> value 3]next:current:valid:
Note:
更多遍历的示例见 SPL 扩展。
Note:
PHP 5.5 及以后版本的用户也可参考生成器,其提供了另一方法来定义 Iterators。
本文地址:https://www.helloaliyun.com/tutorial/285.html
上一篇:PHP的类与对象中文手册:重载 下一篇:PHP的类与对象中文手册:魔术方法