23:PHP遍历对象

php遍历对象

遍历对象

PHP 5 提供了一种定义对象的方法使其可以通过单元列表来遍历,例如用 foreach语句。默认情况下,所有可见属性都将被用于遍历。

  1. <?php
  2.  class  MyClass
  3.  {
  4.     public  $var1  =  'value 1' ;
  5.     public  $var2  =  'value 2' ;
  6.     public  $var3  =  'value 3' ;
  7.     protected  $protected  =  'protected var' ;
  8.     private    $private    =  'private var' ;
  9.     function  iterateVisible () {
  10.        echo  "MyClass::iterateVisible:\n" ;
  11.        foreach$this  as  $key  =>  $value ) {
  12.            print  " $key  =>  $value \n" ;
  13.        }
  14.     }
  15. }
  16. $class  = new  MyClass ();
  17. foreach$class  as  $key  =>  $value ) {
  18.     print  " $key  =>  $value \n" ;
  19. }
  20. echo  "\n" ;
  21. $class -> iterateVisible ();
  22. /**
  23. var1 => value 1
  24. var2 => value 2
  25. var3 => value 3MyClass::iterateVisible:
  26. var1 => value 1
  27. var2 => value 2
  28. var3 => value 3
  29. protected => protected var
  30. private => private var
  31. **/

 

更进一步,可以实现 Iterator 接口。可以让对象自行决定如何遍历以及每次遍历时那些值可用。

  1. <?php
  2.  class  MyIterator  implements  Iterator
  3.  {
  4.     private  $var  = array();
  5.     public function  __construct ( $array )
  6.     {
  7.         if ( is_array ( $array )) {
  8.              $this -> var  =  $array ;
  9.         }
  10.     }
  11.     public function  rewind () {
  12.         echo  "rewinding\n" ;
  13.          reset ( $this -> var );
  14.     }
  15.     public function  current () {
  16.          $var  =  current ( $this -> var );
  17.         echo  "current:  $var \n" ;
  18.         return  $var ;
  19.     }
  20.     public function  key () {
  21.          $var  =  key ( $this -> var );
  22.         echo  "key:  $var \n" ;
  23.         return  $var ;
  24.     }
  25.     public function  next () {
  26.          $var  =  next ( $this -> var );
  27.         echo  "next:  $var \n" ;
  28.         return  $var ;
  29.     }
  30.     public function  valid () {
  31.          $var  =  $this -> current () !==  false ;
  32.         echo  "valid:  { $var } \n" ;
  33.         return  $var ;
  34.     }
  35. }
  36. $values  = array( 1 , 2 , 3 );
  37.  $it  = new  MyIterator ( $values );
  38. foreach ( $it  as  $a  =>  $b ) {
  39.     print  " $a :  $b \n" ;
  40. }
  41. /**
  42. 以上例程会输出:
  43. rewinding
  44. current: 1
  45. valid: 1
  46. current: 1
  47. key: 0
  48. 0: 1
  49. next: 2
  50. current: 2
  51. valid: 1
  52. current: 2
  53. key: 1
  54. 1: 2
  55. next: 3
  56. current: 3
  57. valid: 1
  58. current: 3
  59. key: 2
  60. 2: 3
  61. next:
  62. current:
  63. valid:
  64. **/

 

可以用 IteratorAggregate 接口以替代实现所有的 Iterator 方法。 IteratorAggregate 只需要实现一个方法 IteratorAggregate::getIterator() ,其应返回一个实现了 Iterator 的类的实例。

  1. <?php
  2.  class  MyCollection  implements  IteratorAggregate
  3.  {
  4.     private  $items  = array();
  5.     private  $count  =  0 ;
  6.      // Required definition of interface IteratorAggregate
  7.      public function  getIterator () {
  8.         return new  MyIterator ( $this -> items );
  9.     }
  10.     public function  add ( $value ) {
  11.          $this -> items [ $this -> count ++] =  $value ;
  12.     }
  13. }
  14. $coll  = new  MyCollection ();
  15.  $coll -> add ( 'value 1' );
  16.  $coll -> add ( 'value 2' );
  17.  $coll -> add ( 'value 3' );
  18. foreach ( $coll  as  $key  =>  $val ) {
  19.     echo  "key/value: [ $key  ->  $val ]\n\n" ;
  20. }
  21.  /**
  22. 以上例程会输出:
  23. rewinding
  24. current: value 1
  25. valid: 1
  26. current: value 1
  27. key: 0
  28. key/value: [0 -> value 1]next: value 2
  29. current: value 2
  30. valid: 1
  31. current: value 2
  32. key: 1
  33. key/value: [1 -> value 2]next: value 3
  34. current: value 3
  35. valid: 1
  36. current: value 3
  37. key: 2
  38. key/value: [2 -> value 3]next:
  39. current:
  40. valid:
  41. **/
    A+
发布日期:2017年01月04日 19:45:58  所属分类:PHP教程
最后更新时间:2017-01-04 19:47:04
付杰
  • ¥ 98.0元
  • 市场价:298.0元
  • ¥ 98.0元
  • 市场价:198.0元
  • ¥ 299.0元
  • 市场价:599.0元
  • ¥ 39.0元
  • 市场价:39.0元

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: