Use of the PHP SPL library

Source: Internet
Author: User
Tags spl rewind

What is 1.SPL?
Spl:standard PHP library PHP Standard Libraries, this starting from the php5.0 built-in components and interfaces, after 5.3 gradually mature. Because it is built into the PHP5 development environment, no configuration is required.
According to the official definition, "a collection of interfaces and classes that is meant to solve standard problems."
However, in the current user, SPL is seen more as a interfaces and classes of the array behavior that enables object to mimic.
SPL extends the PHP engine, such as interfaces such as arrayaccess, countable, and seekableiterator, which are used to manipulate objects in the form of arrays. You can also use other iterators such as recursiveiterator,arrayobjects to iterate over the array.
He also built in several objects, such as Exceptions,splobserver,spltorage and splautoloadregister,splclasses,iteratorapply, to overload the corresponding functions.
2.Iterator
The core concept of SPL is iterator, which refers to a design pattern, "provide an object which traverses some aggregate structure,abstracting away Assumptions about the implementation of that structure. "
In layman's words, iterator can make many different data structures with a unified interface, such as a result set of a database, a set of files in the same directory, or a set of each line in a text.
SPL stipulates that all classes that have the iterator interface deployed can be used in a foreach loop. The iterator interface contains the following five methods that must be deployed:

    • Current ()

      This method returns the current index ' s value. You are solely
      Responsible for tracking what's the current index is as the
      Interface does not does this for you.

      • Key ()

        This method returns the value of the current index ' s key. For
        foreach loops This was extremely important so, 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 are a current
        Element. It is called after rewind () or next ().

Arrayaccess interface
Deploy the Arrayaccess interface so that object can operate like an array, but must contain four methods that must be deployed

  • Offsetexists ($offset)
    This method was 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 was used to return the value specified
    By the key offset.
  • Offsetset ($offset, $value)
    This method was used to set a value within the object,
    You can throw an exception from the This function for a
    Read-only collection.
  • Offsetunset ($offset)
    This method was used when a value was removed from
    an array either through unset () or Assig Ning the key
    a value of NULL. In the case of numerical arrays, this
    offset should not being deleted and the array should
    not being reindexed unless th At are specifically the
    behavior you want.
    Iteratoraggregate interface
    Recursiveiterator interface
    This interface is used to traverse multiple layers of data, inherit the iterator interface, and thus have standard current ()/key ()/next () and valid ( Method It also prescribes the GetChildren () and HasChildren () methods themselves. The
    Seekableiterator interface
    Seekableiterator interface is also an extension of the iterator interface, which, in addition to the five methods of iterator, specifies the Seek () method, where the parameter is the position of the element and returns the element. If the position does not exist, OutOfBoundsException is thrown.
    Countable interface
    This interface specifies a count () method that returns the number of result sets
    3.SPL Classes
    SPL built-in class
    View all built-in classes
    foreach (Spl_classes () as $key = = $val) {
    echo $key. = ". $val. ' <br/> ';
    }
    Directoryiterator class
    This class is used to view all files and subdirectories in a directory
    foreach (New Directoryiterator ('./') as $Item)
    {
    Echo $Item. ' <br/> ';
    }
    catch (Exception $e)
    {
    echo ' No files found! ';
    }

Arrayobject class
This class converts an array to object

Arrayiterator class
This class is actually a supplement to the Arrayobject class, providing the latter with traversal functionality. The offset class method and the count () method are also supported

Recursivearrayiterator class and Recursiveiteratoriterator class
The Arrayiterator class and the Arrayobject class, only support traversing one-dimensional arrays, if you want to traverse a multidimensional array, you must first generate a iterator with Recursiveiteratoriterator, And then use Recursiveiteratoriterator for this iterator.
Filteriterator
The Filteriterator class can filter the elements, as long as you set the filter in the Accept () method.

Simplexmliterator class
This class is used to traverse the XML file
Cachingiterator class
This class has a hasnext () method to determine if there is a next element
Limititerator class
This class is used to qualify the number and position of the returned result set, and must provide the offset and limit two parameters, similar to the limit statement in the SQL command
Splfileobject class
This class is used to traverse a text file

Use of the PHP SPL library

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.