色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

php遍歷對象的方法

瀏覽:39日期:2022-09-07 15:09:39

對于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已經為我們準備好了相關的接口,我們只需要去實現這個接口就可以完成迭代器模式的創建了。具體的內容可以參考之前的設計模式系列文章:PHP設計模式之迭代器模式

// 實現迭代器接口class B implements Iterator{ private $var = []; public function __construct($array) {if (is_array($array)) { $this->var = $array;} } public function rewind() {echo 'rewindingn';reset($this->var); } public function current() {$var = current($this->var);echo 'current: $varn';return $var; } public function key() {$var = key($this->var);echo 'key: $varn';return $var; } public function next() {$var = next($this->var);echo 'next: $varn';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:

假如今天的文章只是講之前講過的迭代器模式,那就太沒意思了,所以,咱們還要來學習一個更有意思的應用。那就是讓對象可以像數組一樣進行操作。這個其實也是使用PHP早已為我們準備好的一個接口:ArrayAccess。

// 讓類可以像數組一樣操作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:

這個接口需要我們實現四個方法:

offsetSet($offset, $value),根據偏移量設置值 offsetExists($offset),根據偏移量確定是否存在內容 offsetUnset($offset),根據偏移量刪除內容 offsetGet($offset),根據依稀量獲取內容

這里的偏移量就是我們常說的下標。通過實現這四個方法,我們就可以像操作數組一樣的操作對象。當然,日常開發中我們可能并不會很經常的使用包括迭代器在內的這些對象遍歷的能力。通常我們會直接去將對象轉換成數組 (array) obj 來進行下一步的操作。不過,在java中,特別是JavaBean中會經常在類的內部有一個 List 為自己的對象來表示自身的集合狀態。通過對比,我們發現PHP也完全可以實現這樣的能力,而且使用迭代器和 ArrayAccess 接口還能夠更方便的實現類似的能力。這是非常有用的一種知識擴展,或許下一個項目中你就能運用上這些能力哦!

測試代碼: github.com/zhangyue050…

以上就是php遍歷對象的方法的詳細內容,更多關于php遍歷對象的資料請關注好吧啦網其它相關文章!

標簽: PHP
相關文章:
主站蜘蛛池模板: 欧美搞黄视频 | 亚洲一区 在线播放 | free性丰满白嫩白嫩的hd | 在线观看亚洲精品专区 | 韩国欧洲一级毛片免费 | 日韩欧美一区二区中文字幕 | 国产成人黄网在线免 | 国产成人精品高清不卡在线 | 一级一级特黄女人精品毛片 | 三级毛片三级毛片 | 中文字幕在线看视频一区二区三区 | 91亚洲免费 | 免费在线观看一级毛片 | 美女被cao免费看在线看网站 | 欧美另类在线观看 | 国产伦精一区二区三区视频 | 另类专区 亚洲 | 成人欧美一级毛片免费观看 | 午夜精品尤物福利视频在线 | 国内一区 | 国产欧美视频一区二区三区 | a级国产乱理伦片在线观看国 | 毛片免费在线 | 国产人成免费视频 | 超级乱淫视频aⅴ播放视频 超级碰碰碰在线观看 | 亚洲视频毛片 | 欧美影院久久 | 成人免费看| 亚洲福利视频精选在线视频 | 美女毛片儿 | 欧美午夜精品久久久久免费视 | 91极品尤物| 另类专区另类专区亚洲 | 中文字幕一区二区精品区 | 国产精品自在欧美一区 | 台湾三级在线播放 | 亚洲国产精品免费 | 亚洲风情第一页 | 欧美亚洲国产成人精品 | 久久精品在线 | 国产精品日本一区二区不卡视频 |