對于php來說,foreach是非常方便好用的一個(gè)語法,幾乎對于每一個(gè)PHPer它都是日常接觸最多的請求之一。那么對象是否能通過foreach來遍歷呢?
答案是肯定的,但是有個(gè)條件,那就是對象的遍歷只能獲得它的公共屬性。
// 普通遍歷 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
不管是方法還是受保護(hù)或者私有的變量,都無法遍歷出來。只有公共的屬性才能被遍歷出來。其實(shí),我們之前在講設(shè)計(jì)模式時(shí)講過的迭代器模式就是專門用來進(jìn)行對象遍歷的,而且PHP已經(jīng)為我們準(zhǔn)備好了相關(guān)的接口,我們只需要去實(shí)現(xiàn)這個(gè)接口就可以完成迭代器模式的創(chuàng)建了。具體的內(nèi)容可以參考之前的設(shè)計(jì)模式系列文章:PHP設(shè)計(jì)模式之迭代器模式
// 實(shí)現(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:
假如今天的文章只是講之前講過的迭代器模式,那就太沒意思了,所以,咱們還要來學(xué)習(xí)一個(gè)更有意思的應(yīng)用。那就是讓對象可以像數(shù)組一樣進(jìn)行操作。這個(gè)其實(shí)也是使用PHP早已為我們準(zhǔn)備好的一個(gè)接口: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:
這個(gè)接口需要我們實(shí)現(xiàn)四個(gè)方法:
- offsetSet($offset, $value),根據(jù)偏移量設(shè)置值
- offsetExists($offset),根據(jù)偏移量確定是否存在內(nèi)容
- offsetUnset($offset),根據(jù)偏移量刪除內(nèi)容
- offsetGet($offset),根據(jù)依稀量獲取內(nèi)容
這里的偏移量就是我們常說的下標(biāo)。通過實(shí)現(xiàn)這四個(gè)方法,我們就可以像操作數(shù)組一樣的操作對象。當(dāng)然,日常開發(fā)中我們可能并不會很經(jīng)常的使用包括迭代器在內(nèi)的這些對象遍歷的能力。通常我們會直接去將對象轉(zhuǎn)換成數(shù)組 (array) obj 來進(jìn)行下一步的操作。不過,在java中,特別是JavaBean中會經(jīng)常在類的內(nèi)部有一個(gè) List 為自己的對象來表示自身的集合狀態(tài)。通過對比,我們發(fā)現(xiàn)PHP也完全可以實(shí)現(xiàn)這樣的能力,而且使用迭代器和 ArrayAccess 接口還能夠更方便的實(shí)現(xiàn)類似的能力。這是非常有用的一種知識擴(kuò)展,或許下一個(gè)項(xiàng)目中你就能運(yùn)用上這些能力哦!