|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2008-03-17 03:47 UTC] kevin at metalaxe dot com
Description:
------------
I'm not sure if this is a bug or not. If not then it is definitely a change request.
When creating a RecursiveIterator that also implements Countable, and using count() with COUNT_RECURSIVE, count will not count recursively.
Reproduce code:
---------------
class MyIterator implements RecursiveIterator, Countable
{
protected $toIterate = NULL;
# Other Iterator functions......
public function hasChildren()
{
return ( count( $this->getChildren() ) > 0 );
}
public function getChildren()
{
return new MyIterator( $this->toIterate[ $this->key() ]->cache );
}
public function count()
{
return count( $this->toIterate );
}
}
$multiObject = array();
$multiObject[0] = new stdClass();
$multiObject[0]->cache = array( 1,2,3,4,5 );
$multiObject[1] = new stdClass();
$multiObject[1]->cache = array( 6,7,8,9,10 );
$multiObject[2] = new stdClass();
$multiObject[2]->cache = array( 11,12,13,14,15 );
print count( new MyIterator( $multiObject ), COUNT_RECURSIVE );
Expected result:
----------------
printed total of "18"
Actual result:
--------------
printed total of "3"
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 18:00:01 2025 UTC |
I figured out how to do this. I guess I wasn't expecting to have to use a different count function for this purpose. Produces expected result: public function count() { return iterator_count( new RecursiveIteratorIterator( $this, RecursiveIteratorIterator::SELF_FIRST ) ); } Since this is open, I would like to turn it in to a feature request though. See below Currently Countable works to redirect count, but there is no way to pass to that count override the second argument of count ( COUNT_RECURSIVE ). It would be nice if this could be passable so that in certain situation one could count only the top and deeper only when necessary. Thanks!