Req->Doc #58102 [Opn->Ver]: Cannot pass COUNT_RECURSIVE to Countable

From: Date: Wed, 01 Apr 2020 13:58:16 +0000
Subject: Req->Doc #58102 [Opn->Ver]: Cannot pass COUNT_RECURSIVE to Countable
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-17453@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 Updated by: cmb@php.net Reported by: kevin at metalaxe dot com Summary: Cannot pass COUNT_RECURSIVE to Countable -Status: Open +Status: Verified -Type: Feature/Change Request +Type: Documentation Problem Package: SPL related Operating System: Win Server 2003 PHP Version: 5.2.1 Block user comment: N Private report: N New Comment: Changing to doc problem, since the manual has to clarify that the $mode is only relevant for arrays. Previous Comments: ------------------------------------------------------------------------ [2014-04-17 14:51:10] 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. ------------------------------------------------------------------------ [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

« previous php.doc.bugs (#17453) next »