首页 > 代码库 > PHP中的iterator

PHP中的iterator

大牛就是大牛。。。。。。没得黑
spl

附上:
PHP模版解析类
峰哥原文
新手文章明显不懂背后原理,但php代码很详细,值得一看也不得不说他付出过努力和有一点点“天分”,如果他能几个月对代码由陌生变熟悉,还有几个调试技巧,这文章新手熟手都值得看看

这几天,我在学习PHP语言中的SPL。
这个东西应该属于PHP中的高级内容,看上去很复杂,但是非常有用,所以我做了长篇笔记。不然记不住,以后要用的时候,还是要从头学起。

由于这是供自己参考的笔记,不是教程,所以写得比较简单,没有多解释。但是我想,如果你是一个熟练的PHP5程序员,应该足以看懂下面的材料,而且会发现它很有用。现在除此之外,网上根本没有任何深入的SPL中文介绍。

PHP SPL笔记
目录
第一部分 简介
1. 什么是SPL?
2. 什么是Iterator?
第二部分 SPL Interfaces
3. Iterator界面
4. ArrayAccess界面
5. IteratorAggregate界面
6. RecursiveIterator界面
7. SeekableIterator界面
8. Countable界面
第三部分 SPL Classes
9. SPL的内置类
10. DirectoryIterator类
11. ArrayObject类
12. ArrayIterator类
13. RecursiveArrayIterator类和RecursiveIteratorIterator类
14. FilterIterator类
15. SimpleXMLIterator类
16. CachingIterator类
17. LimitIterator类
18. SplFileObject类
第一部 简介
1. 什么是SPL?
SPL是Standard PHP Library(PHP标准库)的缩写。
根据官方定义,它是”a collection of interfaces and classes that are meant to solve standard problems”。但是,目前在使用中,SPL更多地被看作是一种使object(物体)模仿array(数组)行为的interfaces和classes。
2. 什么是Iterator?
SPL的核心概念就是Iterator。这指的是一种Design Pattern,根据《Design Patterns》一书的定义,Iterator的作用是”provide an object which traverses some aggregate structure, abstracting away assumptions about the implementation of that structure.”
wikipedia中说,”an iterator is an object which allows a programmer to traverse through all the elements of a collection, regardless of its specific implementation”…….”the iterator pattern is a design pattern in which iterators are used to access the elements of an aggregate object sequentially without exposing its underlying representation”.
通俗地说,Iterator能够使许多不同的数据结构,都能有统一的操作界面,比如一个数据库的结果集、同一个目录中的文件集、或者一个文本中每一行构成的集合。
如果按照普通情况,遍历一个MySQL的结果集,程序需要这样写:

// Fetch the “aggregate structure”
$result = mysql_query(“SELECT * FROM users”);

// Iterate over the structure
while ( row=mysqlfetcharray(result) ) {
   // do stuff with the row here
}

读出一个目录中的内容,需要这样写:

// Fetch the “aggregate structure”
$dh = opendir(‘/home/harryf/files’);

// Iterate over the structure
while ( file=readdir(dh) ) {
   // do stuff with the file here
}

读出一个文本文件的内容,需要这样写:

// Fetch the “aggregate structure”
$fh = fopen(“/home/hfuecks/files/results.txt”, “r”);

// Iterate over the structure
while (!feof($fh)) {

line=fgets(fh);
   // do stuff with the line here

}

上面三段代码,虽然处理的是不同的resource(资源),但是功能都是遍历结果集(loop over contents),因此Iterator的基本思想,就是将这三种不同的操作统一起来,用同样的命令界面,处理不同的资源。
第二部分 SPL Interfaces
3. Iterator界面
SPL规定,所有部署了Iterator界面的class,都可以用在foreach Loop中。Iterator界面中包含5个必须部署的方法:

* current()

  This method returns the current index‘s value. You are solely
  responsible for tracking what the current index is as the 
 interface does not do this for you.

* key()

  This method returns the value of the current index‘s key. For 
  foreach loops this is extremely important so that the key 
  value can be populated.

* next()

  This method moves the internal index forward one entry.

* rewind()

  This method should reset the internal index to the first element.

* valid()

  This method should return true or false if there is a current 
  element. It is called after rewind() or next().

下面就是一个部署了Iterator界面的class示例:

/**
* An iterator for native PHP arrays, re-inventing the wheel
*
* Notice the “implements Iterator” - important!
*/
class ArrayReloaded implements Iterator {

/**
   * A native PHP array to iterate over
   */
private $array = array();

/**
   * A switch to keep track of the end of the array
   */
private $valid = FALSE;

/**
   * Constructor
   * @param array native PHP array to iterate over
   */
function __construct(array) {this->array = $array;
}

/**
   * Return the array “pointer” to the first element
   * PHP’s reset() returns false if the array has no elements
   */
function rewind(){
   this?>valid=(FALSE!==reset(this->array));
}

/**
   * Return the current array element
   */
function current(){
   return current($this->array);
}

/**
   * Return the key of the current array element
   */
function key(){
   return key($this->array);
}

/**
   * Move forward by one
   * PHP’s next() returns false if there are no more elements
   */
function next(){
   this?>valid=(FALSE!==next(this->array));
}

/**
   * Is the current element valid?
   */
function valid(){
   return $this->valid;
}
}

使用方法如下:

// Create iterator object
$colors = new ArrayReloaded(array (‘red’,’green’,’blue’,));

// Iterate away!
foreach ( colorsascolor ) {
echo $color.” ”;
}

你也可以在foreach循环中使用key()方法:

// Display the keys as well
foreach ( colorsaskey => color ) {    echo “key: $color ”;
}

除了foreach循环外,也可以使用while循环,

// Reset the iterator - foreach does this automatically
$colors->rewind();

// Loop while valid
while ( $colors->valid() ) {

echo colors?>key().":".colors->current().”
“;
   $colors->next();

}

根据测试,while循环要稍快于foreach循环,因为运行时少了一层中间调用。
4. ArrayAccess界面
部署ArrayAccess界面,可以使得object像array那样操作。ArrayAccess界面包含四个必须部署的方法:

* offsetExists($offset)

  This method is used to tell php if there is a value
  for the key specified by offset. It should return 
  true or false.

* offsetGet($offset)

  This method is used to return the value specified 
  by the key offset.

* offsetSet($offset, $value)

  This method is used to set a value within the object, 
  you can throw an exception from this function for a 
  read-only collection.

* offsetUnset($offset)

  This method is used when a value is removed from 
  an array either through unset() or assigning the key 
  a value of null. In the case of numerical arrays, this 
  offset should not be deleted and the array should 
  not be reindexed unless that is specifically the 
  behavior you want.

下面就是一个部署ArrayAccess界面的实例:

/**
* A class that can be used like an array
*/
class Article implements ArrayAccess {

public $title;

public $author;

public $category; 

function __construct(title,author,category) {this->title = title;this->author = author;this->category = $category;
}

/**
* Defined by ArrayAccess interface
* Set a value given it’s key e.g. A[title]=foo;?@parammixedkey(stringorinteger)?@parammixedvalue?@returnvoid?/functionoffsetSet(key, value) {      if ( array_key_exists(key,get_object_vars(this)) ) {this->{key} =value;
   }
}

/**
* Defined by ArrayAccess interface
* Return a value given it’s key e.g. echo A[title];?@parammixedkey(stringorinteger)?@returnmixedvalue?/functionoffsetGet(key) {
   if ( array_key_exists(key,getobjectvars(this)) ) {
     return this->{key};
   }
}

/**
* Defined by ArrayAccess interface
* Unset a value by it’s key e.g. unset(A[title]);?@parammixedkey(stringorinteger)?@returnvoid?/functionoffsetUnset(key) {
   if ( array_key_exists(key,getobjectvars(this)) ) {
     unset(this->{key});
   }
}

/**
* Defined by ArrayAccess interface
* Check value exists, given it’s key e.g. isset(A[title])?@parammixedkey(stringorinteger)?@returnboolean?/functionoffsetExists(offset) {
   return array_key_exists(offset,getobjectvars(this));
}

}

使用方法如下:

// Create the object
$A = new Article(‘SPL Rocks’,’Joe Bloggs’, ‘PHP’);

// Check what it looks like
echo ‘Initial State:

‘;
print_r($A);
echo ‘
‘;

 

// Change the title using array syntax
$A[‘title’] = ‘SPL really rocks’;

// Try setting a non existent property (ignored)
$A[‘not found’] = 1;

// Unset the author field
unset($A[‘author’]);

// Check what it looks like again
echo ‘Final State:

‘;
print_r($A);
echo ‘
‘;

 

运行结果如下:

Initial State:

Article Object
(
   [title] => SPL Rocks
   [author] => Joe Bloggs
   [category] => PHP
)

Final State:

Article Object
(
   [title] => SPL really rocks
   [category] => PHP
)

可以看到,$A虽然是一个object,但是完全可以像array那样操作。
你还可以在读取数据时,增加程序内部的逻辑:

function offsetGet(key) {      if ( array_key_exists(key,get_object_vars(this)) ) {        return strtolower(this->{$key});
   }
}

  1. IteratorAggregate界面
    但是,虽然$A可以像数组那样操作,却无法使用foreach遍历,除非部署了前面提到的Iterator界面。
    另一个解决方法是,有时会需要将数据和遍历部分分开,这时就可以部署IteratorAggregate界面。它规定了一个getIterator()方法,返回一个使用Iterator界面的object。
    还是以上一节的Article类为例:

class Article implements ArrayAccess, IteratorAggregate {

/**
* Defined by IteratorAggregate interface
* Returns an iterator for for this object, for use with foreach
* @return ArrayIterator
*/
function getIterator() {
   return new ArrayIterator($this);
}

使用方法如下:

$A = new Article(‘SPL Rocks’,’Joe Bloggs’, ‘PHP’);

// Loop (getIterator will be called automatically)
echo ‘Looping with foreach:

‘;
foreach ( Aasfield => value ) {    echo “field : $value ”;
}
echo ‘
‘;

 

// Get the size of the iterator (see how many properties are left)
echo “Object has “.sizeof($A->getIterator()).” elements”;

显示结果如下:

Looping with foreach:

title : SPL Rocks
author : Joe Bloggs
category : PHP

Object has 3 elements

  1. RecursiveIterator界面
    这个界面用于遍历多层数据,它继承了Iterator界面,因而也具有标准的current()、key()、next()、 rewind()和valid()方法。同时,它自己还规定了getChildren()和hasChildren()方法。The getChildren() method must return an object that implements RecursiveIterator.
  2. SeekableIterator界面
    SeekableIterator界面也是Iterator界面的延伸,除了Iterator的5个方法以外,还规定了seek()方法,参数是元素的位置,返回该元素。如果该位置不存在,则抛出OutOfBoundsException。
    下面是一个是实例:

    大牛就是大牛。。。。。。没得黑
    spl

    附上:
    PHP模版解析类
    峰哥原文
    新手文章明显不懂背后原理,但php代码很详细,值得一看也不得不说他付出过努力和有一点点“天分”,如果他能几个月对代码由陌生变熟悉,还有几个调试技巧,这文章新手熟手都值得看看

    这几天,我在学习PHP语言中的SPL。
    这个东西应该属于PHP中的高级内容,看上去很复杂,但是非常有用,所以我做了长篇笔记。不然记不住,以后要用的时候,还是要从头学起。

    由于这是供自己参考的笔记,不是教程,所以写得比较简单,没有多解释。但是我想,如果你是一个熟练的PHP5程序员,应该足以看懂下面的材料,而且会发现它很有用。现在除此之外,网上根本没有任何深入的SPL中文介绍。

    PHP SPL笔记
    目录
    第一部分 简介
    1. 什么是SPL?
    2. 什么是Iterator?
    第二部分 SPL Interfaces
    3. Iterator界面
    4. ArrayAccess界面
    5. IteratorAggregate界面
    6. RecursiveIterator界面
    7. SeekableIterator界面
    8. Countable界面
    第三部分 SPL Classes
    9. SPL的内置类
    10. DirectoryIterator类
    11. ArrayObject类
    12. ArrayIterator类
    13. RecursiveArrayIterator类和RecursiveIteratorIterator类
    14. FilterIterator类
    15. SimpleXMLIterator类
    16. CachingIterator类
    17. LimitIterator类
    18. SplFileObject类
    第一部 简介
    1. 什么是SPL?
    SPL是Standard PHP Library(PHP标准库)的缩写。
    根据官方定义,它是”a collection of interfaces and classes that are meant to solve standard problems”。但是,目前在使用中,SPL更多地被看作是一种使object(物体)模仿array(数组)行为的interfaces和classes。
    2. 什么是Iterator?
    SPL的核心概念就是Iterator。这指的是一种Design Pattern,根据《Design Patterns》一书的定义,Iterator的作用是”provide an object which traverses some aggregate structure, abstracting away assumptions about the implementation of that structure.”
    wikipedia中说,”an iterator is an object which allows a programmer to traverse through all the elements of a collection, regardless of its specific implementation”…….”the iterator pattern is a design pattern in which iterators are used to access the elements of an aggregate object sequentially without exposing its underlying representation”.
    通俗地说,Iterator能够使许多不同的数据结构,都能有统一的操作界面,比如一个数据库的结果集、同一个目录中的文件集、或者一个文本中每一行构成的集合。
    如果按照普通情况,遍历一个MySQL的结果集,程序需要这样写:

    // Fetch the “aggregate structure”
    $result = mysql_query(“SELECT * FROM users”);

    // Iterate over the structure
    while ( row=mysqlfetcharray(result) ) {
       // do stuff with the row here
    }

    读出一个目录中的内容,需要这样写:

    // Fetch the “aggregate structure”
    $dh = opendir(‘/home/harryf/files’);

    // Iterate over the structure
    while ( file=readdir(dh) ) {
       // do stuff with the file here
    }

    读出一个文本文件的内容,需要这样写:

    // Fetch the “aggregate structure”
    $fh = fopen(“/home/hfuecks/files/results.txt”, “r”);

    // Iterate over the structure
    while (!feof($fh)) {

    line=fgets(fh);
       // do stuff with the line here

    }

    上面三段代码,虽然处理的是不同的resource(资源),但是功能都是遍历结果集(loop over contents),因此Iterator的基本思想,就是将这三种不同的操作统一起来,用同样的命令界面,处理不同的资源。
    第二部分 SPL Interfaces
    3. Iterator界面
    SPL规定,所有部署了Iterator界面的class,都可以用在foreach Loop中。Iterator界面中包含5个必须部署的方法:

    * current()
    
      This method returns the current index‘s value. You are solely
      responsible for tracking what the current index is as the 
     interface does not do this for you.
    
    * key()
    
      This method returns the value of the current index‘s key. For 
      foreach loops this is extremely important so that the key 
      value can be populated.
    
    * next()
    
      This method moves the internal index forward one entry.
    
    * rewind()
    
      This method should reset the internal index to the first element.
    
    * valid()
    
      This method should return true or false if there is a current 
      element. It is called after rewind() or next().
    

    下面就是一个部署了Iterator界面的class示例:

    /**
    * An iterator for native PHP arrays, re-inventing the wheel
    *
    * Notice the “implements Iterator” - important!
    */
    class ArrayReloaded implements Iterator {

    /**
       * A native PHP array to iterate over
       */
    private $array = array();

    /**
       * A switch to keep track of the end of the array
       */
    private $valid = FALSE;

    /**
       * Constructor
       * @param array native PHP array to iterate over
       */
    function __construct(array) {this->array = $array;
    }

    /**
       * Return the array “pointer” to the first element
       * PHP’s reset() returns false if the array has no elements
       */
    function rewind(){
       this?>valid=(FALSE!==reset(this->array));
    }

    /**
       * Return the current array element
       */
    function current(){
       return current($this->array);
    }

    /**
       * Return the key of the current array element
       */
    function key(){
       return key($this->array);
    }

    /**
       * Move forward by one
       * PHP’s next() returns false if there are no more elements
       */
    function next(){
       this?>valid=(FALSE!==next(this->array));
    }

    /**
       * Is the current element valid?
       */
    function valid(){
       return $this->valid;
    }
    }

    使用方法如下:

    // Create iterator object
    $colors = new ArrayReloaded(array (‘red’,’green’,’blue’,));

    // Iterate away!
    foreach ( colorsascolor ) {
    echo $color.” ”;
    }

    你也可以在foreach循环中使用key()方法:

    // Display the keys as well
    foreach ( colorsaskey => color ) {    echo “key: $color ”;
    }

    除了foreach循环外,也可以使用while循环,

    // Reset the iterator - foreach does this automatically
    $colors->rewind();

    // Loop while valid
    while ( $colors->valid() ) {

    echo colors?>key().":".colors->current().”
    “;
       $colors->next();

    }

    根据测试,while循环要稍快于foreach循环,因为运行时少了一层中间调用。
    4. ArrayAccess界面
    部署ArrayAccess界面,可以使得object像array那样操作。ArrayAccess界面包含四个必须部署的方法:

    * offsetExists($offset)
    
      This method is used to tell php if there is a value
      for the key specified by offset. It should return 
      true or false.
    
    * offsetGet($offset)
    
      This method is used to return the value specified 
      by the key offset.
    
    * offsetSet($offset, $value)
    
      This method is used to set a value within the object, 
      you can throw an exception from this function for a 
      read-only collection.
    
    * offsetUnset($offset)
    
      This method is used when a value is removed from 
      an array either through unset() or assigning the key 
      a value of null. In the case of numerical arrays, this 
      offset should not be deleted and the array should 
      not be reindexed unless that is specifically the 
      behavior you want.
    

    下面就是一个部署ArrayAccess界面的实例:

    /**
    * A class that can be used like an array
    */
    class Article implements ArrayAccess {

    public $title;

    public $author;

    public $category; 

    function __construct(title,author,category) {this->title = title;this->author = author;this->category = $category;
    }

    /**
    * Defined by ArrayAccess interface
    * Set a value given it’s key e.g. A[title]=foo;?@parammixedkey(stringorinteger)?@parammixedvalue?@returnvoid?/functionoffsetSet(key, value) {      if ( array_key_exists(key,get_object_vars(this)) ) {this->{key} =value;
       }
    }

    /**
    * Defined by ArrayAccess interface
    * Return a value given it’s key e.g. echo A[title];?@parammixedkey(stringorinteger)?@returnmixedvalue?/functionoffsetGet(key) {
       if ( array_key_exists(key,getobjectvars(this)) ) {
         return this->{key};
       }
    }

    /**
    * Defined by ArrayAccess interface
    * Unset a value by it’s key e.g. unset(A[title]);?@parammixedkey(stringorinteger)?@returnvoid?/functionoffsetUnset(key) {
       if ( array_key_exists(key,getobjectvars(this)) ) {
         unset(this->{key});
       }
    }

    /**
    * Defined by ArrayAccess interface
    * Check value exists, given it’s key e.g. isset(A[title])?@parammixedkey(stringorinteger)?@returnboolean?/functionoffsetExists(offset) {
       return array_key_exists(offset,getobjectvars(this));
    }

    }

    使用方法如下:

    // Create the object
    $A = new Article(‘SPL Rocks’,’Joe Bloggs’, ‘PHP’);

    // Check what it looks like
    echo ‘Initial State:

    ‘;
    print_r($A);
    echo ‘
    ‘;

     

    // Change the title using array syntax
    $A[‘title’] = ‘SPL really rocks’;

    // Try setting a non existent property (ignored)
    $A[‘not found’] = 1;

    // Unset the author field
    unset($A[‘author’]);

    // Check what it looks like again
    echo ‘Final State:

    ‘;
    print_r($A);
    echo ‘
    ‘;

     

    运行结果如下:

    Initial State:

    Article Object
    (
       [title] => SPL Rocks
       [author] => Joe Bloggs
       [category] => PHP
    )

    Final State:

    Article Object
    (
       [title] => SPL really rocks
       [category] => PHP
    )

    可以看到,$A虽然是一个object,但是完全可以像array那样操作。
    你还可以在读取数据时,增加程序内部的逻辑:

    function offsetGet(key) {      if ( array_key_exists(key,get_object_vars(this)) ) {        return strtolower(this->{$key});
       }
    }

    1. IteratorAggregate界面
      但是,虽然$A可以像数组那样操作,却无法使用foreach遍历,除非部署了前面提到的Iterator界面。
      另一个解决方法是,有时会需要将数据和遍历部分分开,这时就可以部署IteratorAggregate界面。它规定了一个getIterator()方法,返回一个使用Iterator界面的object。
      还是以上一节的Article类为例:

    class Article implements ArrayAccess, IteratorAggregate {

    /**
    * Defined by IteratorAggregate interface
    * Returns an iterator for for this object, for use with foreach
    * @return ArrayIterator
    */
    function getIterator() {
       return new ArrayIterator($this);
    }

    使用方法如下:

    $A = new Article(‘SPL Rocks’,’Joe Bloggs’, ‘PHP’);

    // Loop (getIterator will be called automatically)
    echo ‘Looping with foreach:

    ‘;
    foreach ( Aasfield => value ) {    echo “field : $value ”;
    }
    echo ‘
    ‘;

     

    // Get the size of the iterator (see how many properties are left)
    echo “Object has “.sizeof($A->getIterator()).” elements”;

    显示结果如下:

    Looping with foreach:

    title : SPL Rocks
    author : Joe Bloggs
    category : PHP

    Object has 3 elements

    1. RecursiveIterator界面
      这个界面用于遍历多层数据,它继承了Iterator界面,因而也具有标准的current()、key()、next()、 rewind()和valid()方法。同时,它自己还规定了getChildren()和hasChildren()方法。The getChildren() method must return an object that implements RecursiveIterator.
    2. SeekableIterator界面
      SeekableIterator界面也是Iterator界面的延伸,除了Iterator的5个方法以外,还规定了seek()方法,参数是元素的位置,返回该元素。如果该位置不存在,则抛出OutOfBoundsException。
      下面是一个是实例:

PHP中的iterator