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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
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

Add a Patch

Pull Requests

Add a Pull Request

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-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 20:01:30 2024 UTC