The SplQueue class

Introduction

The SplQueue class provides the main functionalities of a queue implemented using a doubly linked list by setting the iterator mode to SplDoublyLinkedList::IT_MODE_FIFO.

Class synopsis

SplQueue
extends SplDoublyLinkedList
/* Inherited constants */
public const int SplDoublyLinkedList::IT_MODE_LIFO;
public const int SplDoublyLinkedList::IT_MODE_FIFO;
public const int SplDoublyLinkedList::IT_MODE_DELETE;
public const int SplDoublyLinkedList::IT_MODE_KEEP;
/* Methods */
public mixed SplQueue::dequeue()
public void SplQueue::enqueue(mixed $value)
/* Inherited methods */
public void add(int $index, mixed $value)
public mixed bottom()
public int count()
public mixed current()
public int getIteratorMode()
public bool isEmpty()
public int key()
public void next()
public bool offsetExists(int $index)
public mixed offsetGet(int $index)
public void offsetSet(intnull $index, mixed $value)
public void offsetUnset(int $index)
public mixed pop()
public void prev()
public void push(mixed $value)
public void rewind()
public string serialize()
public int setIteratorMode(int $mode)
public mixed shift()
public mixed top()
public void unserialize(string $data)
public void unshift(mixed $value)
public bool valid()

Examples

Example #1 SplQueue example

<?php
$q = new SplQueue();
$q[] = 1;
$q[] = 2;
$q[] = 3;
foreach ($q as $elem)  {
 echo $elem."\n";
}
?>

The above example will output:

1
2
3

Example #2 Efficiently handling tasks with SplQueue

<?php
$q = new SplQueue();
$q->setIteratorMode(SplQueue::IT_MODE_DELETE);
// ... enqueue some tasks on the queue ...
// process them
foreach ($q as $task) {
    // ... process $task ...
    // add new tasks on the queue
    $q[] = $newTask;
    // ...
}
?>

Table of Contents