]> granicus.if.org Git - php/commitdiff
- Remove unused variable
authorMarcus Boerger <helly@php.net>
Thu, 29 Apr 2004 07:22:02 +0000 (07:22 +0000)
committerMarcus Boerger <helly@php.net>
Thu, 29 Apr 2004 07:22:02 +0000 (07:22 +0000)
- Respect visibility in count() and add a test for that

ext/spl/spl_array.c
ext/spl/tests/array_012.phpt [new file with mode: 0755]

index a4a5465d7c79f086a27155e511208894ddb4a643..73063fe95dbdb99d3a11228fa75cf2dd6f671027 100755 (executable)
@@ -723,18 +723,31 @@ SPL_METHOD(Array, seek)
  Return the number of elements in the Iterator. */
 SPL_METHOD(Array, count)
 {
-       long position;
        zval *object = getThis();
        spl_array_object *intern = (spl_array_object*)zend_object_store_get_object(object TSRMLS_CC);
        HashTable *aht = HASH_OF(intern->array);
        HashPosition pos;
+       long cnt;
 
        if (!aht) {
                php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Array was modified outside object and is no longer an array");
                RETURN_LONG(0);
        }
 
-       RETURN_LONG(zend_hash_num_elements(aht));
+       if (Z_TYPE_P(intern->array) == IS_OBJECT) {
+               pos = intern->pos;
+               cnt = 0;
+               zend_hash_internal_pointer_reset_ex(aht, &intern->pos);
+               while(intern->pos) {
+                       cnt++;
+                       spl_array_next(intern TSRMLS_CC);
+               }
+               intern->pos = pos;
+               RETURN_LONG(cnt);
+       } else {
+               RETURN_LONG(zend_hash_num_elements(aht));
+       }
+       
 } /* }}} */
 
 /* {{{ proto mixed|NULL ArrayIterator::current()
diff --git a/ext/spl/tests/array_012.phpt b/ext/spl/tests/array_012.phpt
new file mode 100755 (executable)
index 0000000..a888965
--- /dev/null
@@ -0,0 +1,65 @@
+--TEST--
+SPL: ArrayIterator::count
+--SKIPIF--
+<?php if (!extension_loaded("spl")) print "skip"; ?>
+--FILE--
+<?php
+
+echo "===Array===\n";
+
+$a = array('zero' => 0, 'one' => 1, 'two' => 2);
+$it = new ArrayIterator($a);
+
+var_dump($it->count());
+foreach($it as $key => $val)
+{
+       echo "$key=>$val\n";
+       var_dump($it->count());
+}
+var_dump($it->count());
+
+echo "===Object===\n";
+
+class test
+{
+       public $zero = 0;
+       protected $pro;
+       public $one = 1;
+       private $pri;
+       public $two = 2;
+}
+
+$o = new test;
+$it = new ArrayIterator($o);
+
+var_dump($it->count());
+foreach($it as $key => $val)
+{
+       echo "$key=>$val\n";
+       var_dump($it->count());
+}
+var_dump($it->count());
+
+?>
+===DONE===
+<?php exit(0); ?>
+--EXPECT--
+===Array===
+int(3)
+zero=>0
+int(3)
+one=>1
+int(3)
+two=>2
+int(3)
+int(3)
+===Object===
+int(3)
+zero=>0
+int(3)
+one=>1
+int(3)
+two=>2
+int(3)
+int(3)
+===DONE===