Php 简明教程

PHP - Object Iteration

一个 foreach 循环可用于遍历 PHP 类的对象的公开可见的所有成员。此功能在 PHP 5 及其以后版本中提供。您当然可以访问实例方法中的私有属性列表。PHP 还定义了可用于此目的的 Iterator 接口。

A foreach loop may be employed to iterate through all the publicly visible members of an object of a PHP class. This feature has been available in versions of PHP 5 onwards. You can of course access the list of private properties inside an instance method. PHP also defines Iterator interface which can be used for the purpose.

Using foreach Loop

在下面的示例中,类的公开属性使用 foreach 循环列出。

In the example below, the public properties of the class are listed with the use of foreach loop.

Example

<?php
   class myclass {
      private $var;
      protected $var1;
      public $x, $y, $z;
      public function __construct() {
         $this->var="Hello World";
         $this->var1=array(1,2,3);
         $this->x=100;
         $this->y=200;
         $this->z=300;
      }
   }
   $obj = new myclass();
   foreach($obj as $key => $value) {
      print "$key => $value\n";
   }
?>

它将生成以下 output

It will produce the following output

x => 100
y => 200
z => 300

请注意,只有公共成员才能在类外访问。如果类包含一个方法,则所有成员(公有、私有或受保护的)都可以从内部通过 foreach 循环进行遍历。

Note that only the public members are accessible outside the class. If the class includes a method, all the members (public, private or protected) can be traversed with a foreach loop from inside it.

让我们在上面的 myclass 中添加一个迭代方法。

Let us add an iterate method in the above myclass.

public function iterate() {
   foreach ($this as $k=>$v) {
      if (is_array($v)) {
         var_dump($v);
         echo PHP_EOL;
      } else {
         echo "$k : $v". PHP_EOL;
      }
   }
}

调用此实例方法以获取所有成员的列表。

Call this instance method to get the list of all the members.

它将生成以下 output

It will produce the following output

var : Hello World
array(3) {
   [0]=>
   int(1)
   [1]=>
   int(2)
   [2]=>
   int(3)
}
x : 100
y : 200
z : 300

Using Iterator Interface

PHP 提供 Iterator 接口,用于可自身在内部进行迭代的外部迭代器或对象。它定义了下面需要在用户定义类中实现的抽象方法。

PHP provides Iterator interface for external iterators or objects that can be iterated themselves internally. It defines following abstract methods which need to be implemented in the user defined class.

interface Iterator extends Traversable {
   /* Methods */
   public current(): mixed
   public key(): mixed
   public next(): void
   public rewind(): void
   public valid(): bool
}
  1. The rewind() method rewinds the Iterator to the first element. This is the first method called when starting a foreach loop. It will not be executed after foreach loops.

  2. The current() method returns the current element.

  3. The key() method returns the key of the current element on each iteration of foreach loop.

  4. The next() method is called after each iteration of foreach loop and moves forward to next element.

  5. The valid() method checks if current position is valid.

Example

下面的示例通过实现 Iterator 接口演示对象迭代

The following example demonstrates object iteration by implementing Iterator interface

<?php
   class myclass implements Iterator {
      private $arr = array('a','b','c');

      public function rewind():void {
         echo "rewinding\n";
         reset($this->arr);
      }

      public function current() {
         $var = current($this->arr);
         echo "current: $var\n";
         return $var;
      }

      public function key() {
         $var = key($this->arr);
         echo "key: $var\n";
         return $var;
      }

      public function next() : void {
         $var = next($this->arr);
         echo "next: $var\n";
         # return $var;
      }

      public function valid() : bool {
         $key = key($this->arr);
         $var = ($key !== NULL && $key !== FALSE);
         echo "valid: $var\n";
         return $var;
      }
   }

   $obj = new myclass();

   foreach ($obj as $k => $v) {
      print "$k: $v\n";
   }
?>

它将生成以下 output

It will produce the following output

rewinding
valid: 1
current: a
key: 0
0: a
next: b
valid: 1
current: b
key: 1
1: b
next: c
valid: 1
current: c
key: 2
2: c
next: