SPL是用於解決典型問題(standard problems)的一組介面與類的集合。
1. SplDoublyLinkedList
原型:
SplDoublyLinkedList implements Iterator , ArrayAccess , Countable {/* 方法 */public __construct ( void )public void add ( mixed $index , mixed $newval )public mixed bottom ( void )public int count ( void )public mixed current ( void )public int getIteratorMode ( void )public bool isEmpty ( void )public mixed key ( void )public void next ( void )public bool offsetExists ( mixed $index )public mixed offsetGet ( mixed $index )public void offsetSet ( mixed $index , mixed $newval )public void offsetUnset ( mixed $index )public mixed pop ( void )public void prev ( void )public void push ( mixed $value )public void rewind ( void )public string serialize ( void )public void setIteratorMode ( int $mode )public mixed shift ( void )public mixed top ( void )public void unserialize ( string $serialized )public void unshift ( mixed $value )public bool valid ( void )}
樣本:
//Constructs a new doubly linked list $list = new SplDoublyLinkedList(); //Pushes an element at the end of the doubly linked list $list -> push('a'); $list -> push('b'); $list -> push('c'); $list -> push('d'); $list -> push('e'); $list -> push('f'); // Counts the number of elements in the doubly linked list. $list -> count(); // Sets the mode of iteration $list -> setIteratorMode(SplDoublyLinkedList::IT_MODE_LIFO); // Returns the mode of iteration $model = $list -> getIteratorMode(); /* IT_MODE_LIFO => int(2) * IT_MODE_FIFO => int(0) * IT_MODE_DELETE => int(1) * IT_MODE_KEEP => int(0) */ // Checks whether the doubly linked list is empty. $list -> isEmpty(); // Return current node index $list->key(); // Move to next entry $list -> next(); // Returns whether the requested $index exists $list -> offsetExists(3); // Returns the value at the specified $index $list -> offsetGet(3); // Sets the value at the specified $index to $newval $list -> offsetSet(3,'s'); // Unsets the value at the specified $index $list -> offsetUnset(3); // Pops a node from the end of the doubly linked list $list -> pop(); // Peeks at the node from the end of the doubly linked list $list -> top(); // Move to previous entry $list -> prev(); // Rewind iterator back to the start $list -> rewind(); // Serializes the storage // $list -> serialize(); // Shifts a node from the beginning of the doubly linked list $list -> shift(); // Prepends the doubly linked list with an element $list -> unshift('s'); // Check whether the doubly linked list contains more nodes $list -> valid(); for($list -> rewind();$list -> valid();$list -> next()){ //Return current array entry echo $list -> current(); }