略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: SplQueue::dequeue

2024-12-22

SplQueue::dequeue

(PHP 5 >= 5.3.0, PHP 7, PHP 8)

SplQueue::dequeueDequeues a node from the queue

说明

public SplQueue::dequeue(): mixed

Dequeues value from the top of the queue.

注意:

SplQueue::dequeue() is an alias of SplDoublyLinkedList::shift().

参数

此函数没有参数。

返回值

The value of the dequeued node.

add a noteadd a note

User Contributed Notes 4 notes

up
10
xuecan at gmail dot com
12 years ago
If the queue is empty, dequeue() will raise an 'RuntimeException' with message 'Can't shift from an empty datastructure'.
up
5
mark at bull-roarer dot com
8 years ago
I just thought this was a fun and interesting way for lining up method calls and then calling them back-to-back. Might be useful as a basis for a transactional execution class or something.

<?php
$q
= new SplQueue();
$q->setIteratorMode(SplQueue::IT_MODE_DELETE);

$q->enqueue(array("FooBar", "foo"));
$q->enqueue(array("FooBar", "bar"));
$q->enqueue(array("FooBar", "msg", "Hi there!"));

foreach (
$q as $task) {
  if (
count($task) > 2) {
    list(
$class, $method, $args) = $task;
   
$class::$method($args);
  } else {
    list(
$class, $method) = $task;
   
$class::$method();
  }
}

class
FooBar {
  public static function
foo() {
    echo
"FooBar::foo() called.\n";
  }
  public static function
bar() {
    echo
"FooBar::bar() called.\n";
  }
  public static function
msg($msg) {
    echo
"$msg\n";
  }
}
?>

Results:
FooBar::foo() called.
FooBar::bar() called.
Hi there!
up
4
andresdzphp at php dot net
10 years ago
<?php
$q
= new SplQueue();
$q->setIteratorMode(SplQueue::IT_MODE_DELETE);
$q->enqueue('item 1');
$q->enqueue('item 2');
$q->enqueue('item 3');

$q->dequeue();
$q->dequeue();

foreach (
$q as $item) {
    echo
$item;
}

//Result: item 3

$q->dequeue(); //Fatal error: Uncaught exception 'RuntimeException'
              //with message 'Can't shift from an empty datastructure'
?>
up
0
mark at bull-roarer dot com
8 years ago
I just thought this was a fun and interesting way for lining up method calls and then calling them back-to-back. Might be useful as a basis for a transactional execution class or something.

<?php
$q
= new SplQueue();
$q->setIteratorMode(SplQueue::IT_MODE_DELETE);

$q->enqueue(array("FooBar", "foo"));
$q->enqueue(array("FooBar", "bar"));
$q->enqueue(array("FooBar", "msg", "Hi there!"));

foreach (
$q as $task) {
  if (
count($task) > 2) {
    list(
$class, $method, $args) = $task;
   
$class::$method($args);
  } else {
    list(
$class, $method) = $task;
   
$class::$method();
  }
}

class
FooBar {
  public static function
foo() {
    echo
"FooBar::foo() called.\n";
  }
  public static function
bar() {
    echo
"FooBar::bar() called.\n";
  }
  public static function
msg($msg) {
    echo
"$msg\n";
  }
}
?>

Results:
FooBar::foo() called.
FooBar::bar() called.
Hi there!

官方地址:https://www.php.net/manual/en/splqueue.dequeue.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3