天天看點

php spl庫的使用

1.SPL 是什麼?

SPL:standard php library php标準庫,此 從php5.0起開始内置的元件和接口,在5.3以後逐漸成熟。因為内置在php5開發環境中,無需任何配置。

根據官方定義,“a collection of interfaces and classes that are meant to solve standard problems.”

然而在目前的使用者,spl更多地被看做是一種使object模仿的array行為的interfaces和classes。

SPL對PHP引擎進行了擴充,例如ArrayAccess、Countable和SeekableIterator等接口,它們用于以數組形式操作對象。同時還可以使用RecursiveIterator,ArrayObjects等其他疊代器進行數組的疊代操作。

他還内置了幾個對象,例如Exceptions,SplObserver,spltorage以及splautoloadregister,splclasses,iteratorapply等的幫助函數,用于重載對應的功能。

2.Iterator

spl的核心概念是Iterator,這指一種設計模式(Design Pattern),"provide an object which traverses some aggregate structure,abstracting away assumptions about the implementation of that structure."

通俗的說,Iterator能夠使許多不同的資料結構,都能有統一的操作界面,比如一個資料庫的結果集、同一目錄的檔案集或者一個文本中每一行構成的集合。

SPL規定,所有部署了Iterator界面的class,都可以用在foreach loop中。Iterator界面包含以下必須部署的五個方法:

  • 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().

ArrayAccess界面

部署ArrayAccess界面,可以使object像Array那樣操作,但是必須包含四個必須部署的方法

  • 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.

    IteratorAggregate界面

    RecursiveIterator界面

    這個界面用于周遊多層資料,繼承了Iterator界面,因而也具有标準的current()/key()/next()和valid()方法。同時它自己還規定了getChildren()和hasChildren()方法。

    SeekableIterator界面

    SeekableIterator界面也是Iterator界面的延伸,除了Iterator的五個方法以外,還規定了seek()方法,參數是元素的位置,傳回該元素。若該位置不存在,則抛出OutOfBoundsException。

    Countable界面

    這個界面規定了一個count()方法,傳回結果集的數量

    3.SPL Classes

    spl内置類

    檢視所有内置類

    foreach(spl_classes() as $key=>$val){

    echo $key."=>".$val.'<br/>';

    }

    DirectoryIterator類

    這個類用來檢視一個目錄中所有檔案和子目錄

    foreach(new DirectoryIterator('./') as $Item)

    {

    echo $Item.'<br/>';

    catch(Exception $e)

    echo 'No files Found!';

ArrayObject類

此類将Array轉換為Object