php中迭代接口的使用,算不上设计模式

之前用java写过迭代设计模式,个人觉得PHP这个迭代没撒子使用作用

<?php
  class collection implements Iterator {
      //被迭代数组   
      private $_iterator_arr;
      //索引值
      private $_index = 0;
      
      public function __construct($arr){
        $this->_iterator_arr = $arr;
      }
      
      public function __destruct(){
          
      }
      /*
      * 验证当前是否有值
      * @author lichaoyong 2014-04-18 16:36:28
      */
      public function valid(){
          return isset($this->_iterator_arr[$this->_index]);
      }
      
      /**
      * 返回当前索引的值
      * @author lichaoyong 2014-04-18 16:37:26
      */
      public function current(){
          return $this->_iterator_arr[$this->_index];
      }
      
      /**
      * 返回当前的索引
      * @author lichaoyong 2014-04-18 16:38:25
      */
      public function key(){
          return $this->_index;
      }
      
      /**
      * 返回下一个索引
      * @author lichaoyong 2014-04-18 16:39:04
      */
      public function next(){
          $this->_index++;
      }
      
      
      /**
      * 把索引指向第一个单元
      * @author lichaoyong 2014-04-18 16:45:17
      */
      public function rewind(){
          $this->_index = 0 ;
      }
      
      
      /**
      * 返回最后一个单元
      * @author lichaoyong 2014-04-18 16:47:37
      */
      public function end(){
          return end($this->_iterator_arr);
      }
      
      
  }
?>

<?php
/**
* 程序主入口
* @author lichaoyong 2014-04-18 16:15:17
* 
* 迭代器设计模式
*/
  class main{
      public function main(){
        
        $array = array('A', 'B', 'C', 'D'=>array(1));  
        echo "Collection: ";  
        $coll_arr = new collection($array);
        $coll_arr->next();
        echo $coll_arr->rewind();
        echo $coll_arr->current();
        echo $coll_arr->end();
        
          
      }
  }
?>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值