PHP SPL (PHP Standard Library)

PHP SPL (PHP Standard Library)

1. What is SPL ?

SPL is a collection of interfaces and classes for solving standard problems . (from: http://php.net/manual/en/book.spl.php )

SPL , PHP Standard Library ( Standard PHP Library ), built-in components and interfaces since PHP 5.0 , and has gradually matured since PHP 5.3 . SPL is built into all PHP5 development environments and requires no setup.

The content mainly includes data structure classes, iterators, exception classes, SPL functions, and some interfaces.
The data structure class mainly includes basic data structures such as stack, team, heap, array, etc. PHP has already packaged it for you. If you want to do data processing, you can use it directly, which is very convenient.
Iterator is a new feature of php, and it is very flexible to use in oop, for example, it can be used to traverse an array of objects.
The exception class mainly encapsulates some common exceptions and makes them into interfaces. In general, there is a template format, and you can extend it according to his requirements.
There is a very important thing in the SPL function, spl_autoload_register(), which is used to implement automatic loading in oop.

 

2. How to use it ?

SPL provides a set of standard data structures:

    Doubly linked list

SplDoublyLinkedList

SplStack

SplQueue

    The double-linked list is an important linear storage structure. For each node in the double-linked list, it not only stores its own information, but also stores the addresses of the predecessor and successor nodes.

 

 

 

 

 

The SplDoublyLinkedList class in PHP SPL provides operations on doubly linked lists.

See attached spl_SplDoublyLinkedList.php .

 

 

heap

SplHeap 

SplMaxHeap

SplMinHeap

    Heap is a data structure designed to implement a priority queue, which is implemented by constructing a binary heap ( a type of binary tree ) . The heap with the largest root node is called the maximum heap or large root heap ( SplMaxHeap ), and the heap with the smallest root node is called the minimum heap or small root heap ( SplMinHeap ). Binary heaps are also commonly used for sorting ( heapsort ) .

The SplHeap class summary is as follows: see attachment spl_SplHeap.php

Obviously he is an abstract class, the maximum heap (SplMaxHeap) and the minimum heap (SplMinHeap) are inherited from it. There are no additional methods for max-heap and min-heap.

Simple and practical, see attachment spl_SplHeap_exe.php

 

 

priority queue

SplPriorityQueue

The priority queue is also a very practical data structure, which can sort the values ​​by weighting. Since the sorting is implemented inside PHP , the business code will be much simpler and more efficient. Set the extraction method via SplPriorityQueue::setExtractFlags(int $flag) to extract data (equivalent to max heap), priority, and both.

The SplPriorityQueue class summary is as follows: spl_SplPriorityQueue.php

Simple to use, see attachment: spl_SplPriorityQueue_exe.php

 

 array

SplFixedArray

SplFixedArray mainly deals with the main functions related to arrays. Different from ordinary php arrays , it is an array of fixed length and uses numbers as key names. The advantage is that it is faster than ordinary array processing. Usually SplFixedArray is 20%~30% faster than php array , so if you are dealing with huge number of fixed length arrays, it is strongly recommended.

A summary of the SplFixedArray class is as follows: spl_SplFixedArray.php

Simple to use: spl_SplFixedArray_exe.php

 

map

SplObjectStorage

Used to store a set of objects, especially when you need to uniquely identify objects.

The PHP SPL SplObjectStorage class implements four interfaces : Countable, Iterator, Serializable, and ArrayAccess . It can realize functions such as statistics, iteration, serialization, and array access.

The SplObjectStorage class summary is as follows: spl_SplObjectStorage.php

Simple to use: spl_SplObjectStorage_exe.php

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325992626&siteId=291194637
php