Skip to content

Instantly share code, notes, and snippets.

@kosinix
Last active September 19, 2016 08:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kosinix/1fc332b686b4b6fe0388f76691684e98 to your computer and use it in GitHub Desktop.
Save kosinix/1fc332b686b4b6fe0388f76691684e98 to your computer and use it in GitHub Desktop.
PHP Gotchas

SplPriorityQueue Does Not Behave Like a Queue

SplPriorityQueue is advertised as a prioritized queue. Ideally, as a queue, when adding items of the same priority, the items should retain the same order as how they were registered (FIFO). In PHP the order is unexpected:

<?php
$queue = new SplPriorityQueue();
$queue->insert('1',0);
$queue->insert('2',0);
$queue->insert('3',0);
$queue->insert('4',0);
$queue->insert('5',0);

while($queue->valid()){
    var_dump( $queue->current() );
    $queue->next();
}

Output:

string(1) "1"
string(1) "5"
string(1) "4"
string(1) "3"
string(1) "2"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment