php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #30113 ArrayAccess interface does not implement count functionality
Submitted: 2004-09-16 14:44 UTC Modified: 2010-12-29 14:04 UTC
Votes:1
Avg. Score:1.0 ± 0.0
Reproduced:0 of 1 (0.0%)
From: kevin at stormtide dot ca Assigned: helly (profile)
Status: Closed Package: SPL related
PHP Version: 5.0.* OS: *
Private report: No CVE-ID: None
 [2004-09-16 14:44 UTC] kevin at stormtide dot ca
Description:
------------
IN SPL: The ArrayAccess interface does not correcly work with the root level count() function. I realise that the ArrayObject interface supposedly does, however that class requires an array as the basis, and thus is not useful for virtual arrays (like abstracting a large db without ever loading it)

Reproduce code:
---------------
class somearray implements ArrayAccess {
  ... overloads and such.
  function length() {
    return count($this->internalarrayorabstract);
  }
}
$inst = new somearray();
$inst[0] = 'foo';
$inst[1] = 'foo';
$inst[2] = 'foo';
$length = $isnt->length;
$countedlength = count($inst);

//$length and $countedlength will differ in value with the latter returning 1 always and the former the correct val.

Expected result:
----------------
$countedlength == $length



Actual result:
--------------
$countedlength == 1

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-10-29 11:52 UTC] kevin at oceania dot net
check your code.
If this is a cut and paste you have typo here:
$length = $isnt->length;
should read
$length = $inst->length;
 [2004-10-29 18:25 UTC] kevin at stormtide dot ca
Sorry for the mockup code. I thought it would be sufficient to describe the problem.

Here is actually executable code.

<?PHP
	error_reporting(E_ALL);

	class NArray Implements ArrayAccess, Iterator {
		
		private $arraylist;
		private $currentIndex;
		
		function __construct() {
			$this->currentIndex = 0;
			$this->arraylist = array();
		}
		
		function offsetExists($offset){
		   if(isset($this->arraylist[$offset])){
		       return TRUE;
		   } else {
		       return FALSE;
		   }
		}

		function offsetGet($offset){
		   return $this->arraylist[$offset];
		}

		function offsetSet($offset, $value){
		   if($offset){
		       $this->arraylist[$offset] = $value;
		   } else {
		       $this->arraylist[] = $value;
		   }
		}

		function offsetUnset($offset){
			unset($this->arraylist[$offset]); 
		}
		
		function current() {
			return $this->arraylist[$this->currentIndex];
		}
		
		function key() {
			return $this->currentIndex;
		}
		
		function next() {
			return $this->arraylist[$this->currentIndex++];
		}
		
		function rewind() {
			$this->currentIndex = 0;
		}
		
		function valid() {
			if(array_key_exists($this->currentIndex, $this->arraylist)) {
				return true;
			} else {
				return false;
			}
		}
		
		function append($value) {
			$this->arraylist->offsetSet(null, $value);
		}
		function length() {
			return count($this->arraylist);
		}

		function getIterator() {
			return $this;
		}

	}
	
	$nArray = new NArray();
	$nArray[0] = 'a';
	$nArray[1] = 'b';
	$nArray[2] = 'c';
	$nArray[3] = 'd';
	
	foreach($nArray as $index => $item) {
		echo $index . ' = '. $item . '<br />';
	}
	
	echo 'Counted Length Outside Class: '. count($nArray) ."<br>";
	echo 'Counted Length Inside Class: '.$nArray->length();
	echo "<br><br><br>";
	show_source(__FILE__);
?>

Please ignore the fact that this should be an arrayobject because it is simply a proof of concept, the use of an arrayaccess interface will be used later to work with a sql dataset without actually loading all the data in that dataset. A count value will be returned from num_rows type commands without an internal array of the data ever existing.

Hope this helps.
 [2004-11-01 11:17 UTC] helly@php.net
Probably PHP 5.1 comes with the following interface which works with count():

Interface Countable
{
    function count()
}
 [2004-11-01 11:45 UTC] helly@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.

Countable is added to 5.1-dev
 [2010-12-29 14:04 UTC] jani@php.net
-Package: PECL related +Package: SPL related
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 15:01:29 2024 UTC