PHP 7.2.0 Alpha 2 Released

کلاس SplQueue


کلاس SplQueue عملکرد اصلی پیاده‌سازی صف را با استفاده لیست دو پیوندی پیاده می‌کند.

Class synopsis

SplQueue extends SplDoublyLinkedList implements Iterator , ArrayAccess , Countable {
/* Methods */
__construct ( void )
mixed dequeue ( void )
void enqueue ( mixed $value )
void setIteratorMode ( int $mode )
/* Inherited methods */
mixed SplDoublyLinkedList::key ( void )
void SplDoublyLinkedList::offsetSet ( mixed $index , mixed $newval )
mixed SplDoublyLinkedList::pop ( void )
mixed SplDoublyLinkedList::top ( void )

Table of Contents

add a note add a note

User Contributed Notes 3 notes

Manu Manjunath
3 years ago
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();

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().
2 years ago
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.

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) {
            throw new
exception("send error: " . socket_last_error($socket));
        if (
$rtn < strlen($senditem) {
$sendQueue->unshift(substr($senditem, $rtn);
8 months ago
Take care that SplQueue::valid() is not returning true if the queue has nodes. Use isEmpty() instead:

$queue = new SplQueue();
var_dump($queue->valid()); // false
var_dump(!$queue->isEmpty()); // true
To Top