PHPKonf: Istanbul PHP Conference 2017


(PHP 5 >= 5.3.0, PHP 7)

SplDoublyLinkedList::getIteratorModeReturns the mode of iteration


public int SplDoublyLinkedList::getIteratorMode ( void )


This function has no parameters.

Return Values

Returns the different modes and flags that affect the iteration.

add a note add a note

User Contributed Notes 1 note

gregorypatmore at gmail dot com
5 years ago
I think it should be noted that the IT_MODE_* constants are defined inside the class as:

IT_MODE_LIFO => int(2)
IT_MODE_FIFO => int(0)
IT_MODE_DELETE => int(1)
IT_MODE_KEEP => int(0)

With the FIFO and KEEP flags both defined as zero, you can run into an ambiguity when trying to discern what modes have been activated when trying to use bitwise deduction as one might be lead to believe considering the bitwise way you set the iterator mode (at least in v5.3.5, haven't tested any others).

Consider this example:


= new SPLDoublyLinkedList();
$l->setIteratorMode(SplDoublyLinkedList::IT_MODE_FIFO | SplDoublyLinkedList::IT_MODE_DELETE);

$mode = $l->getIteratorMode(); #int(1)

var_dump(($mode & SplDoublyLinkedList::IT_MODE_FIFO) == SplDoublyLinkedList::IT_MODE_FIFO); #outputs true
var_dump(($mode & SplDoublyLinkedList::IT_MODE_LIFO) == SplDoublyLinkedList::IT_MODE_LIFO); #outputs false
var_dump(($mode & SplDoublyLinkedList::IT_MODE_DELETE) == SplDoublyLinkedList::IT_MODE_DELETE); #outputs true
var_dump(($mode & SplDoublyLinkedList::IT_MODE_KEEP) == SplDoublyLinkedList::IT_MODE_KEEP); #outputs true (huh?!)


Regardless of whatever the getIteratorMode tells you, the object DOES correctly honor the configuration that was set as far as I can tell. 

The way you SHOULD do it (at least until they add any more options):

Basically, you should just check the non-zero flags (LIFO(2) and DELETE(1)).  Since it's an either/or situation within the pairs, this should be able to help you figure out the separate mode pieces an instance has been set to.


= new SPLDoublyLinkedList();
$l->setIteratorMode(SplDoublyLinkedList::IT_MODE_FIFO | SplDoublyLinkedList::IT_MODE_DELETE);

$mode = $l->getIteratorMode();

$isLIFO = ($mode & SplDoublyLinkedList::IT_MODE_LIFO) == SplDoublyLinkedList::IT_MODE_LIFO;

$isDELETE = ($mode & SplDoublyLinkedList::IT_MODE_DELETE) == SplDoublyLinkedList::IT_MODE_DELETE;

Hope this can help someone.
To Top