|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #70016 SplDoublyLinkedList should return ::key() for key()
Submitted: 2015-07-08 03:49 UTC Modified: 2021-07-15 13:57 UTC
From: me at evertpot dot com Assigned: nikic (profile)
Status: Wont fix Package: SPL related
PHP Version: 5.5.26 OS: Any
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
Solve the problem:
39 - 31 = ?
Subscribe to this entry?

 [2015-07-08 03:49 UTC] me at evertpot dot com
See code sample =)

Test script:

$foo = new SplDoublyLinkedList();


Expected result:

Actual result:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2015-08-18 18:44 UTC]
-Type: Bug +Type: Feature/Change Request
 [2015-08-18 18:44 UTC]
This is not a bug, see the signature of key():

| mixed key ( array &$array )
 [2015-08-18 21:39 UTC] me at evertpot dot com
current, next and key work on every Traversable. The signature in the docs is simply *also* a bug, albeit a documentation bug.

See here for an alternative example using ArrayAccess:

This has been a PHP feature since the introduction of Iterator.
 [2015-08-18 22:43 UTC]
> current, next and key work on every Traversable.

Albeit not necessarily in a meaningful way, see

> The signature in the docs is simply *also* a bug, albeit a
> documentation bug.

Well, I don't think so, because key() internally expects a
HashTable as argument[1]. Passing an arbitrary Traversable might
cause undesirable overhead (and maybe worse; consider generators).

Anyway, as you are insisting that this is a bug, I'm changing back
to bug.

[1] <>
 [2015-08-18 22:43 UTC]
-Type: Feature/Change Request +Type: Bug
 [2015-08-18 23:36 UTC] me at evertpot dot com
You were right. I was under the assumption that key() mapped consistently to Iterator::key(), but this is not true at all.

In addition, I was also under the assumption that for generators, this syntax:

yield $key => $value

would also map to key(), but this is not true either. I also found out that neither current() nor next() actually give meaningful results on the result of a generator function. That does seem like a bug, as it works for other iterators.

So I'm sorry for the back and forward. I know you guys must get a lot of reports. While I still think that key(), current() and next() should *just* work for any iterator, generator result or array, you were right that at least in the case of key() this was never the case for *most* iterators. So, up to you how you want to treat this.
 [2015-08-26 21:23 UTC]
-Summary: SplDoublyLinkedList returns NULL for key() +Summary: SplDoublyLinkedList should return ::key() for key() -Type: Bug +Type: Feature/Change Request
 [2015-08-26 21:23 UTC]
I still don't think this is a bug, so I'm switching back to
feature request.

I don't deny that it might be nice to extend key(), current() and
next() to work on all Traversables (even though I personally very
rarely use these functions at all), several other array functions
should also be extended the same way for even more consistency,
see also
 [2021-04-19 12:49 UTC]
-Assigned To: +Assigned To: nikic
 [2021-04-19 12:49 UTC]
Assigning to self to track deprecation (
 [2021-07-15 13:57 UTC]
-Status: Assigned +Status: Wont fix
 [2021-07-15 13:57 UTC]
Deprecation has landed, so this is declined.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Feb 23 08:01:29 2024 UTC