php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #63678 SplObjectStorage->current() !== current(SplObjectStorage)
Submitted: 2012-12-03 21:07 UTC Modified: 2012-12-09 01:46 UTC
From: Alex at phpguide dot co dot il Assigned:
Status: Duplicate Package: SPL related
PHP Version: 5.4.9 OS: debian 6
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: Alex at phpguide dot co dot il
New email:
PHP Version: OS:

 

 [2012-12-03 21:07 UTC] Alex at phpguide dot co dot il
Description:
------------
$SplObjectStorage->current() returns different value from
current($SplObjectStorage).


Test script:
---------------
class test
{
	public $val;
	
	public function __construct()
	{
		$this->val = rand();
	}
}

class blat extends \SplObjectStorage
{
	public function add()
	{
		$this->attach(new test());
	}
}

$blat = new blat();
$blat->add();
$blat->add();
$blat->add();
$blat->add();

var_dump($blat->current() === current($blat));
// null === false

Expected result:
----------------
true

Actual result:
--------------
false

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-12-04 00:25 UTC] mail+php at requinix dot net
SplObjectStorage provides ->current() as part of the implementation to 
Iterator/Traversable. If you want to call it directly you may need to obey the 
Iterator convention of calling ->rewind() first.

But doing so only changes the ->current() value to be the object and not null. 
Even with $blat->rewind() and reset($blat) both key() and current() behave 
differently. In the example for SplObjectStorage::current() it includes

$object = $s->current(); // similar to current($s)

so I would also expect them to behave the same.

tl;dr with ->rewind() and reset():
key($blat)==null, current($blat)==false
$blat->key()==0, $blat->current()==a test object

Without ->rewind() and reset():
key($blat)==null, current($blat)==false
$blat->key()==0, $blat->current()==null
 [2012-12-09 01:46 UTC] levim@php.net
-Status: Open +Status: Duplicate
 [2012-12-09 01:46 UTC] levim@php.net
This is a duplicate of #49369 (https://bugs.php.net/bug.php?id=49369).
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jan 05 01:01:28 2025 UTC