php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #58102 Cannot pass COUNT_RECURSIVE to Countable
Submitted: 2008-03-17 03:47 UTC Modified: 2020-04-01 13:58 UTC
Votes:4
Avg. Score:3.0 ± 0.0
Reproduced:3 of 3 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: kevin at metalaxe dot com Assigned:
Status: Verified Package: SPL related
PHP Version: 5.2.1 OS: Win Server 2003
Private report: No CVE-ID: None
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 !
Your email address:
MUST BE VALID
Solve the problem:
5 + 40 = ?
Subscribe to this entry?

 
 [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"

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-03-17 21:36 UTC] kevin at metalaxe dot com
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!
 [2014-03-27 15:22 UTC] levim@php.net
-Package: SPL +Package: SPL related
 [2014-04-17 14:51 UTC] levim@php.net
Personally I believe it is up to the implementing class to determine if `count` should return a recursive count or not. Just my $0.02.
 [2020-04-01 13:58 UTC] cmb@php.net
-Status: Open +Status: Verified -Type: Feature/Change Request +Type: Documentation Problem
 [2020-04-01 13:58 UTC] cmb@php.net
Changing to doc problem, since the manual has to clarify that the
$mode is only relevant for arrays.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 01 04:01:29 2024 UTC