mirror of
https://github.com/php/php-src.git
synced 2024-11-29 04:46:07 +08:00
- Remove unused variable
- Respect visibility in count() and add a test for that
This commit is contained in:
parent
17d544632e
commit
bf5f758c93
@ -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()
|
||||
|
65
ext/spl/tests/array_012.phpt
Executable file
65
ext/spl/tests/array_012.phpt
Executable file
@ -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===
|
Loading…
Reference in New Issue
Block a user