phpspl庫的使用-創(chuàng)新互聯(lián)

1.SPL 是什么?
SPL:standard php library php標(biāo)準(zhǔn)庫,此 從php5.0起開始內(nèi)置的組件和接口,在5.3以后逐漸成熟。因?yàn)閮?nèi)置在php5開發(fā)環(huán)境中,無需任何配置。
根據(jù)官方定義,“a collection of interfaces and classes that are meant to solve standard problems.”
然而在目前的使用者,spl更多地被看做是一種使object模仿的array行為的interfaces和classes。
SPL對PHP引擎進(jìn)行了擴(kuò)展,例如ArrayAccess、Countable和SeekableIterator等接口,它們用于以數(shù)組形式操作對象。同時還可以使用RecursiveIterator,ArrayObjects等其他迭代器進(jìn)行數(shù)組的迭代操作。
他還內(nèi)置了幾個對象,例如Exceptions,SplObserver,spltorage以及splautoloadregister,splclasses,iteratorapply等的幫助函數(shù),用于重載對應(yīng)的功能。
2.Iterator
spl的核心概念是Iterator,這指一種設(shè)計模式(Design Pattern),"provide an object which traverses some aggregate structure,abstracting away assumptions about the implementation of that structure."
通俗的說,Iterator能夠使許多不同的數(shù)據(jù)結(jié)構(gòu),都能有統(tǒng)一的操作界面,比如一個數(shù)據(jù)庫的結(jié)果集、同一目錄的文件集或者一個文本中每一行構(gòu)成的集合。
SPL規(guī)定,所有部署了Iterator界面的class,都可以用在foreach loop中。Iterator界面包含以下必須部署的五個方法:

創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站設(shè)計、網(wǎng)站制作、陽城網(wǎng)絡(luò)推廣、微信平臺小程序開發(fā)、陽城網(wǎng)絡(luò)營銷、陽城企業(yè)策劃、陽城品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們大的嘉獎;創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供陽城建站搭建服務(wù),24小時服務(wù)熱線:028-86922220,官方網(wǎng)址:muchs.cn
  • 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界面
    這個界面用于遍歷多層數(shù)據(jù),繼承了Iterator界面,因而也具有標(biāo)準(zhǔn)的current()/key()/next()和valid()方法。同時它自己還規(guī)定了getChildren()和hasChildren()方法。
    SeekableIterator界面
    SeekableIterator界面也是Iterator界面的延伸,除了Iterator的五個方法以外,還規(guī)定了seek()方法,參數(shù)是元素的位置,返回該元素。若該位置不存在,則拋出OutOfBoundsException。
    Countable界面
    這個界面規(guī)定了一個count()方法,返回結(jié)果集的數(shù)量
    3.SPL Classes
    spl內(nèi)置類
    查看所有內(nèi)置類
    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轉(zhuǎn)換為Object

ArrayIterator類
這個類實(shí)際上是對ArrayObject類的補(bǔ)充,為后者提供遍歷功能。也支持offset類方法和count()方法

RecursiveArrayIterator類和RecursiveIteratorIterator類
ArrayIterator類和ArrayObject類,只支持遍歷一維數(shù)組,如果要遍歷多維數(shù)組,必須先用RecursiveIteratorIterator生成一個Iterator,然后再對這個Iterator使用RecursiveIteratorIterator
FilterIterator
FilterIterator類可以對元素進(jìn)行過濾,只要在accept()方法中設(shè)置過濾條件就可以了。

SimpleXMLIterator類
這個類用來遍歷xml文件
CachingIterator類
這個類有一個hasNext()方法,用來判斷是否還有下一個元素
LimitIterator類
這個類用來限定返回結(jié)果集的數(shù)量和位置,必須提供offset和limit兩個參數(shù),與SQL命令中的limit語句類似
SplFileObject類
這個類用來對文本文件進(jìn)行遍歷

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

文章標(biāo)題:phpspl庫的使用-創(chuàng)新互聯(lián)
轉(zhuǎn)載源于:http://muchs.cn/article46/dejshg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、響應(yīng)式網(wǎng)站、動態(tài)網(wǎng)站、企業(yè)網(wǎng)站制作、手機(jī)網(wǎng)站建設(shè)、移動網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化