The SplQueue class

(PHP 5 >= 5.3.0, PHP 7)

简介

SplQueue 类通过使用一个双向链表来提供队列的主要功能。

类摘要

SplQueue extends SplDoublyLinkedList implements Iterator , ArrayAccess , Countable {
/* 方法 */
__construct ( void )
dequeue ( void ) : mixed
enqueue ( mixed $value ) : void
setIteratorMode ( int $mode ) : void
/* 继承的方法 */
public SplDoublyLinkedList::add ( mixed $index , mixed $newval ) : void
public SplDoublyLinkedList::count ( void ) : int
public SplDoublyLinkedList::isEmpty ( void ) : bool
public SplDoublyLinkedList::key ( void ) : mixed
public SplDoublyLinkedList::next ( void ) : void
public SplDoublyLinkedList::offsetExists ( mixed $index ) : bool
public SplDoublyLinkedList::offsetSet ( mixed $index , mixed $newval ) : void
public SplDoublyLinkedList::offsetUnset ( mixed $index ) : void
public SplDoublyLinkedList::pop ( void ) : mixed
public SplDoublyLinkedList::prev ( void ) : void
public SplDoublyLinkedList::push ( mixed $value ) : void
public SplDoublyLinkedList::rewind ( void ) : void
public SplDoublyLinkedList::serialize ( void ) : string
public SplDoublyLinkedList::setIteratorMode ( int $mode ) : void
public SplDoublyLinkedList::top ( void ) : mixed
public SplDoublyLinkedList::unserialize ( string $serialized ) : void
public SplDoublyLinkedList::unshift ( mixed $value ) : void
public SplDoublyLinkedList::valid ( void ) : bool
}

Table of Contents

User Contributed Notes

lincoln dot du dot j at gmail dot com 12-Jul-2017 07:07
<?php
 
$queue
= new SplQueue();
$queue->enqueue('A');
$queue->enqueue('B');
$queue->enqueue('C');

$queue->rewind();
while(
$queue->valid()){
    echo
$queue->current(),"\n";
   
$queue->next();
}

print_r($queue);
$queue->dequeue(); //remove first one
print_r($queue);

?>
Output

A
B
C
SplQueue Object
(
    [flags:SplDoublyLinkedList:private] => 4
    [dllist:SplDoublyLinkedList:private] => Array
        (
            [0] => A
            [1] => B
            [2] => C
        )

)
SplQueue Object
(
    [flags:SplDoublyLinkedList:private] => 4
    [dllist:SplDoublyLinkedList:private] => Array
        (
            [0] => B
            [1] => C
        )

)
Stingus 23-Sep-2016 10:16
Take care that SplQueue::valid() is not returning true if the queue has nodes. Use isEmpty() instead:

$queue = new SplQueue();
$queue->enqueue('A');
$queue->enqueue('B');
$queue->enqueue('C');
var_dump($queue->valid()); // false
var_dump(!$queue->isEmpty()); // true
MrStonedOne 31-Dec-2014 04:49
You can use shift/unshift and push/pop to dequeue/undequeue and queue/unqueue respectively. Really handy for those applications that use sockets where you might not know you can't send data until you attempt to.

for example, this is a function for an application that will un-dequeue the remainder of the data if socket_write indicated it did not write the entire contents of the provided data.

<?php
function processSendQueue($socket, $sendQueue) {
    while (!
$sendQueue->isEmpty()) {
                           
//shift() is the same as dequeue()
       
$senditem = $sendQueue->shift();

       
//returns the number of bytes written.
       
$rtn = socket_write($socket, $senditem);
        if (
$rtn === false) {
           
$sendQueue->unshift($senditem);
            throw new
exception("send error: " . socket_last_error($socket));
            return;
        }
        if (
$rtn < strlen($senditem) {
           
$sendQueue->unshift(substr($senditem, $rtn);
            break;
        }
    }
}
?>
Manu Manjunath 10-Feb-2014 10:56
SplQueue inherits from SplDoublyLinkedList. So, objects of SplQueue support methods push() and pop(). But please be advised that if you use push() and pop() methods on a SplQueue object, it behaves like a stack rather than a queue.

For example:

$q = new SplQueue();
$q->push(1);
$q->push(2);
$q->push(3);
$q->pop();
print_r($q);

Above code returns:

SplQueue Object
(
    [flags:SplDoublyLinkedList:private] => 4
    [dllist:SplDoublyLinkedList:private] => Array
        (
            [0] => 1
            [1] => 2
        )
)

Note that 3 got popped and *not* 1.

So, please make sure that you use only enqueue() and dequeue() methods on a SplQueue object and *not* push() and pop().