PHP 物件迭代
PHP 5 provides a way for objects to be defined so it is possible to iterate through a list of items, with, for example a foreach statement. By default, allvisible properties will be used for the iteration. PHP5提供了一種迭代(iteration)物件的功能,就像使用陣列那樣,可以通過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: ";
foreach($this as $key => $value) {
print "$key => $value ";
}
}
}
$class = new MyClass();
foreach($class as $key => $value) {
print "$key => $value ";
}
echo " ";
$class->iterateVisible();
?>
以上例程會輸出:
var1 => value 1 var2 => value 2 var3 => value 3 MyClass::iterateVisible: var1 => value 1 var2 => value 2 var3 => value 3 protected => protected var private => private var
如上所示, the foreach 遍歷了所有 可見的 屬性. 你也可以通過實現PHP 5自帶的 Iterator介面來實現迭代. 使用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 ";
reset($this->var);
}
public function current() {
$var = current($this->var);
echo "current: $var ";
return $var;
}
public function key() {
$var = key($this->var);
echo "key: $var ";
return $var;
}
public function next() {
$var = next($this->var);
echo "next: $var ";
return $var;
}
public function valid() {
$var = $this->current() !== false;
echo "valid: {$var} ";
return $var;
}
}
$values = array(1,2,3);
$it = new MyIterator($values);
foreach ($it as $a => $b) {
print "$a: $b ";
}
?>
以上例程會輸出:
rewinding current: 1 valid: 1 current: 1 key: 0 0: 1 next: 2 current: 2 valid: 1 current: 2 key: 1 1: 2 next: 3 current: 3 valid: 1 current: 3 key: 2 2: 3 next: current: valid:
你也可以讓類實現IteratorAggregate介面,這樣你的類就不用強制性地實現 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]
";
}
?>
以上例程會輸出:
rewinding current: value 1 valid: 1 current: value 1 key: 0 key/value: [0 -> value 1] next: value 2 current: value 2 valid: 1 current: value 2 key: 1 key/value: [1 -> value 2] next: value 3 current: value 3 valid: 1 current: value 3 key: 2 key/value: [2 -> value 3] next: current: valid:
相關文章
- 可迭代物件、迭代器、生成器物件
- PHP 迭代器 (轉)PHP
- Python可迭代的物件與迭代器Python物件
- python可迭代物件Python物件
- 可迭代物件 vs 迭代器 vs 生成器物件
- Python生成器、迭代器、可迭代物件Python物件
- PHP迭代器Iterator介面PHP
- Python之可迭代物件、迭代器、生成器Python物件
- PHP的SPL擴充套件庫(二)物件陣列與陣列迭代器PHP套件物件陣列
- 你知道JavaScript中的可迭代物件與迭代器嗎JavaScript物件
- json中物件陣列迭代JSON物件陣列
- 搞清楚 Python 的迭代器、可迭代物件、生成器Python物件
- 跟你深入剖析可迭代物件和迭代器的區別與聯絡物件
- 迭代器與可迭代物件的區別,以及iter()函式的使用。物件函式
- PHP的迭代器和生成器PHP
- 關於python中可迭代物件和迭代器的一些理解Python物件
- 關於我對可迭代物件,迭代器,生成器的一些理解物件
- Python 3.x自定義迭代器物件Python物件
- 理解php物件注入PHP物件
- PHP物件導向PHP物件
- PHP 建立流式物件PHP物件
- Python中可迭代物件、迭代器以及iter()函式的兩個用法詳解Python物件函式
- PHP的SPL擴充套件庫(三)迭代器PHP套件
- PHP設計模式漫談之迭代器模式PHP設計模式
- 重要內建函式、常見內建函式(瞭解)、可迭代物件、迭代器物件、for迴圈原理、異常捕獲函式物件
- Python基礎(四)——迭代器/物件,生成器Python物件
- 第一類物件(函式),閉包及迭代器物件函式
- php中有沒有物件PHP物件
- php.類與物件PHP物件
- PHP 建立鏈式物件PHP物件
- PHP物件導向(三)PHP物件
- PHP物件的比較PHP物件
- PHP物件的引用及物件優化策略PHP物件優化
- 遍歷聚合物件中的元素——迭代器模式(四)物件模式
- 遍歷聚合物件中的元素——迭代器模式(三)物件模式
- PHP物件導向深入研究之【物件生成】PHP物件
- javascript物件和php物件和json區別JavaScript物件PHPJSON
- WordPress < 3.6.1 PHP 物件注入漏洞PHP物件