Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Standard PHP Library and Classes

The SplStack, SplQueue and SplDoublyLinkedList classes

Reference for PHP SplStack, SplQueue and SplDoublyLinkedList: push, pop, enqueue, dequeue, iteration order, empty-structure exceptions and when arrays are enough.

SplDoublyLinkedList is a list that you can add to and remove from at both ends in constant time. Two classes build on it: SplStack (last in, first out) and SplQueue (first in, first out). Their method names say what they are for, and they iterate in the order that fits their purpose.

A stack suits undo history, parsing and depth-first search. A queue suits jobs, messages and breadth-first search.

PHP
<?php
$stack = new SplStack();
$stack->push('a');
$stack->push('b');
echo $stack->pop();

Try it

PHP SplStack: last in, first out

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP SplQueue and SplDoublyLinkedList

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
class SplDoublyLinkedList implements Iterator, Countable, ArrayAccess, Serializable { /* ... */ }
class SplQueue extends SplDoublyLinkedList { /* ... */ }
class SplStack extends SplDoublyLinkedList { /* ... */ }
Method Description
push($value) Adds to the end (the top of a stack).
pop() Removes and returns the last element.
unshift($value) Adds to the front.
shift() Removes and returns the first element.
top() / bottom() Reads the last / first element without removing it.
enqueue($value) / dequeue() SplQueue names for push() and shift().
isEmpty(), count() Emptiness and size.
offsetGet($i), offsetSet($i, $v), offsetUnset($i), add($i, $v) Access by position; add() inserts.
setIteratorMode($mode) IT_MODE_FIFO or IT_MODE_LIFO, combined with IT_MODE_DELETE or IT_MODE_KEEP.

Return value: pop(), shift(), top(), bottom() and dequeue() return the element.

Errors / Exceptions: removing or peeking at an empty structure throws RuntimeException. An invalid index throws OutOfRangeException. SplStack and SplQueue do not allow changing the direction of iteration (RuntimeException).

Usage notes

  • Iteration order. A stack iterates last-in-first-out, a queue first-in-first-out. Iterating does not remove items unless IT_MODE_DELETE is set.
  • top() is the last element. For a queue, bottom() shows the next item to be dequeued.
  • Arrays can do this too. array_pop() and array_push() are fine for stacks. array_shift() renumbers the array, which is slow for large queues; SplQueue::dequeue() does not.
  • Keys are positions. The indexes in foreach are numbers counted from the bottom of the list, not stored keys.
  • Indexing is not constant-time. offsetGet($i) walks the links; use an array if you need fast random access.
  • Check before removing. Test isEmpty() or catch RuntimeException; these classes never return null to signal emptiness.
  • Stored values are not typed. Wrap them in your own class if you want a typed stack.
  • Copying: assignment shares the object; use clone for an independent copy.