helly           Thu Apr 29 03:22:02 2004 EDT

  Added files:                 
    /php-src/ext/spl/tests      array_012.phpt 

  Modified files:              
    /php-src/ext/spl    spl_array.c 
  Log:
  - Remove unused variable
  - Respect visibility in count() and add a test for that
  
  
http://cvs.php.net/diff.php/php-src/ext/spl/spl_array.c?r1=1.42&r2=1.43&ty=u
Index: php-src/ext/spl/spl_array.c
diff -u php-src/ext/spl/spl_array.c:1.42 php-src/ext/spl/spl_array.c:1.43
--- php-src/ext/spl/spl_array.c:1.42    Wed Apr 28 17:45:41 2004
+++ php-src/ext/spl/spl_array.c Thu Apr 29 03:22:02 2004
@@ -16,7 +16,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: spl_array.c,v 1.42 2004/04/28 21:45:41 helly Exp $ */
+/* $Id: spl_array.c,v 1.43 2004/04/29 07:22:02 helly Exp $ */
 
 #ifdef HAVE_CONFIG_H
 # include "config.h"
@@ -723,18 +723,31 @@
  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()

http://cvs.php.net/co.php/php-src/ext/spl/tests/array_012.phpt?r=1.1&p=1
Index: php-src/ext/spl/tests/array_012.phpt
+++ php-src/ext/spl/tests/array_012.phpt
--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===

-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to