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界面包含以下必須部署的五個方法:
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那樣操作,但是必須包含四個必須部署的方法
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)
猜你還喜歡下面的內(nèi)容