php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #48052 SPLStack::setIteratorMode() throw exception on keep/delete flag
Submitted: 2009-04-22 16:50 UTC Modified: 2009-05-06 15:29 UTC
From: ralph at smashlabs dot com Assigned: colder (profile)
Status: Not a bug Package: SPL related
PHP Version: 5.3, 6CVS (2009-04-25) OS: *
Private report: No CVE-ID: None
 [2009-04-22 16:50 UTC] ralph at smashlabs dot com
Description:
------------
Attempting to change the IteratorMode throws an exception.

The is probably due to the code that changes the LIFO/FIFO ordering that is also settable via the inherited method of SplDoublyLinkedList::setIteratorMode().



Reproduce code:
---------------
<?php

$s = new SPLStack(); 
$s->setIteratorMode(SplDoublyLinkedList::IT_MODE_DELETE);

Expected result:
----------------
Do not throw exception on the following use cases:

->setIteratorMode(SplDoublyLinkedList::IT_MODE_DELETE)
->setIteratorMode(SplDoublyLinkedList::IT_MODE_KEEP)

Actual result:
--------------
PHP Fatal error:  Uncaught exception 'RuntimeException' with message 'Iterators' LIFO/FIFO modes for SplStack/SplQueue objects are frozen' in path/to/test-splstack.php:4
Stack trace:
#0 path/to/test-splstack.php(4): SplDoublyLinkedList->setIteratorMode(1)
#1 {main}
  thrown in path/to/test-splstack.php on line 4

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-05-02 13:29 UTC] colder@php.net
The reason is that setIteratorMode replaces the iterator flags entirely, so you need to specify the order of iteration as well if you want to change the KEEP/DELETE.


Since you use a stack, simply use:
SplDoublyLinkedList:: IT_MODE_DELETE | SplDoublyLinkedList::IT_MODE_LIFO


 [2009-05-06 15:29 UTC] ralph at smashlabs dot com
Thats fine, and that works.

I guess this needs to be documented inside of setIteratorMode(), I would have never guessed that flags are replaced, my initial assumption would be that they are OR'd with the existing flags.

Thanks for clearing this up.
-ralph
 [2018-07-09 20:55 UTC] claudio at z-files dot de
While SplStack throws an exception on setting the keep/delete flag, SplQueue is fine with those changes. They both do have a different behaviour for setIteratorMode().
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 11:01:30 2024 UTC