设为首页 收藏本站
查看: 948|回复: 0

[经验分享] php spl库的使用

[复制链接]
发表于 2018-12-11 11:40:29 | 显示全部楼层 |阅读模式
  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.'';
    }
    DirectoryIterator类
    这个类用来查看一个目录中所有文件和子目录
    foreach(new DirectoryIterator('./') as $Item)
    {
    echo $Item.'';
    }
    catch(Exception $e)
    {
    echo 'No files Found!';
    }
  ArrayObject类
此类将Array转换为Object
  ArrayIterator类
这个类实际上是对ArrayObject类的补充,为后者提供遍历功能。也支持offset类方法和count()方法
  RecursiveArrayIterator类和RecursiveIteratorIterator类
ArrayIterator类和ArrayObject类,只支持遍历一维数组,如果要遍历多维数组,必须先用RecursiveIteratorIterator生成一个Iterator,然后再对这个Iterator使用RecursiveIteratorIterator
FilterIterator
FilterIterator类可以对元素进行过滤,只要在accept()方法中设置过滤条件就可以了。
  SimpleXMLIterator类
这个类用来遍历xml文件
CachingIterator类
这个类有一个hasNext()方法,用来判断是否还有下一个元素
LimitIterator类
这个类用来限定返回结果集的数量和位置,必须提供offset和limit两个参数,与SQL命令中的limit语句类似
SplFileObject类
这个类用来对文本文件进行遍历




运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-650070-1-1.html 上篇帖子: PHP编程之收入支出明细表实现技术 下篇帖子: 简述MVC思想 与PHP如何实现MVC
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表