對于php來說,foreach是非常方便好用的一個語法,幾乎對于每一個PHPer它都是日常接觸最多的請求之一。那么對象是否能通過foreach來遍歷呢?
答案是肯定的,但是有個條件,那就是對象的遍歷只能獲得它的公共屬性。
// 普通遍歷 class A { public $a1 = '1'; public $a2 = '2'; public $a3 = '3'; private $a4 = '4'; protected $a5 = '5'; public $a6 = '6'; public function test() { echo 'test'; } } $a = new A(); foreach ($a as $k => $v) { echo $k, '===', $v, PHP_EOL; } // a1===1 // a2===2 // a3===3 // a6===6
不管是方法還是受保護或者私有的變量,都無法遍歷出來。只有公共的屬性才能被遍歷出來。其實,我們之前在講設計模式時講過的迭代器模式就是專門用來進行對象遍歷的,而且PHP已經(jīng)為我們準備好了相關的接口,我們只需要去實現(xiàn)這個接口就可以完成迭代器模式的創(chuàng)建了。具體的內(nèi)容可以參考之前的設計模式系列文章:PHP設計模式之迭代器模式
// 實現(xiàn)迭代器接口 class B implements Iterator { private $var = []; 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; } } $b = new B([1, 2, 3, 4]); foreach ($b as $k => $v) { echo $k, '===', $v, PHP_EOL; } // 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: 4 // current: 4 // valid: 1 // current: 4 // key: 3 // 3===4 // next: // current: // valid:
假如今天的文章只是講之前講過的迭代器模式,那就太沒意思了,所以,咱們還要來學習一個更有意思的應用。那就是讓對象可以像數(shù)組一樣進行操作。這個其實也是使用PHP早已為我們準備好的一個接口:ArrayAccess。
// 讓類可以像數(shù)組一樣操作 class C implements ArrayAccess, IteratorAggregate { private $container = []; public function __construct() { $this->container = [ "one" => 1, "two" => 2, "three" => 3, ]; } public function offsetSet($offset, $value) { if (is_null($offset)) { $this->container[] = $value; } else { $this->container[$offset] = $value; } } public function offsetExists($offset) { return isset($this->container[$offset]); } public function offsetUnset($offset) { unset($this->container[$offset]); } public function offsetGet($offset) { return isset($this->container[$offset]) ? $this->container[$offset] : null; } public function getIterator() { return new B($this->container); } } $c = new C(); var_dump($c); $c['four'] = 4; var_dump($c); $c[] = 5; $c[] = 6; var_dump($c); foreach($c as $k=>$v){ echo $k, '===', $v, PHP_EOL; } // rewinding // current: 1 // valid: 1 // current: 1 // key: one // one===1 // next: 2 // current: 2 // valid: 1 // current: 2 // key: two // two===2 // next: 3 // current: 3 // valid: 1 // current: 3 // key: three // three===3 // next: 4 // current: 4 // valid: 1 // current: 4 // key: four // four===4 // next: 5 // current: 5 // valid: 1 // current: 5 // key: 0 // 0===5 // next: 6 // current: 6 // valid: 1 // current: 6 // key: 1 // 1===6 // next: // current: // valid:
這個接口需要我們實現(xiàn)四個方法:
- offsetSet($offset, $value),根據(jù)偏移量設置值
- offsetExists($offset),根據(jù)偏移量確定是否存在內(nèi)容
- offsetUnset($offset),根據(jù)偏移量刪除內(nèi)容
- offsetGet($offset),根據(jù)依稀量獲取內(nèi)容
這里的偏移量就是我們常說的下標。通過實現(xiàn)這四個方法,我們就可以像操作數(shù)組一樣的操作對象。當然,日常開發(fā)中我們可能并不會很經(jīng)常的使用包括迭代器在內(nèi)的這些對象遍歷的能力。通常我們會直接去將對象轉換成數(shù)組 (array) obj 來進行下一步的操作。不過,在java中,特別是JavaBean中會經(jīng)常在類的內(nèi)部有一個 List 為自己的對象來表示自身的集合狀態(tài)。通過對比,我們發(fā)現(xiàn)PHP也完全可以實現(xiàn)這樣的能力,而且使用迭代器和 ArrayAccess 接口還能夠更方便的實現(xiàn)類似的能力。這是非常有用的一種知識擴展,或許下一個項目中你就能運用上這些能力哦!