SPL classes

Undoubtedly, SPL tries to solve common data structure implementation issues for PHP programmers. Many of us are either afraid or reluctant to implement proper data structure while programming. SPL comes with implementation of all basic data structure and, hence, makes life easier for developers by using built-in classes and methods. Since SPL comes as a bundle with PHP, we do not need to install it separately or enable any extension for it. In this section, we will discuss some of the common SPL classes in brief:

  • SplDoublyLinkedList: This class gives us the option to implement a doubly linked list without writing a big chunk of code. Though it says doubly linked list, we can utilize this class to implement stack and queue as well, by setting the iteration mode in the setIteratorMode method.
  • SplStack: SplStack class is an extended version of the SplDoublyLinkedList class where the standard stack functions are available, which are actually from the doubly linked list class.
  • SplQueue: SplQueue class is an extended version of the SplDoublyLinkedList class where the standard queue functions such as enqueue, dequeue are available. However, these functions are actually from the doubly linked list class.
  • SplHeap: This is a generic heap implementation for PHP. SplMaxHeap and SplMinHeap are two implementations from the generic heap class.
  • SplPriorityQueue: SplPriorityQueue is implemented using SplMaxHeap and provides basic functionalities of a priority queue.
  • SplFixedArray: As we have seen in Chapter 2, Understanding PHP Arrays, SplFixedArray can be very handy to resolve memory and performance issues. SplFixedArray takes integer as index, and hence, it has faster read and write operations compared to generic PHP array.
  • SplObjectStorage: Usually, we store anything in array either using integer or string key. This SPL class provides us with a way to store a value against an object. In object storage, we can use the object directly as a key for mapping. Also, we can use this class to store object collection.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset