Req #58102 [Com]: Cannot pass COUNT_RECURSIVE to Countable

From: Date: Thu, 17 Apr 2014 14:51:11 +0000
Subject: Req #58102 [Com]: Cannot pass COUNT_RECURSIVE to Countable
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-185331@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=58102&edit=1

 ID:                 58102
 Comment by:         levim@php.net
 Reported by:        kevin at metalaxe dot com
 Summary:            Cannot pass COUNT_RECURSIVE to Countable
 Status:             Open
 Type:               Feature/Change Request
 Package:            SPL related
 Operating System:   Win Server 2003
 PHP Version:        5.2.1
 Block user comment: N
 Private report:     N

 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------
[2008-03-17 21:36:25] 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!

------------------------------------------------------------------------
[2008-03-17 03:47:37] 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"


------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=58102&edit=1


Thread (1 message)

  • levim@php.net
  • Unknown Message
    • levim@php.net
« previous php.bugs (#185331) next »