
ArrayIterator 클래스는 Iterator 인터페이스를 구현한 클래스로, 배열을 반복할 때 사용됩니다. Iterator::count 메서드는 Iterator 인터페이스에 정의된 메서드입니다. 따라서 ArrayIterator 클래스도 이 메서드를 상속받아 사용할 수 있습니다.
ArrayIterator::count 메서드와 Iterator::count 메서드는 차이가 없습니다. 둘 다 배열의 요소 개수를 반환합니다.
ArrayIterator::count 메서드를 사용하는 경우, 다음과 같이 구현할 수 있습니다.
#hostingforum.kr
php
$array = [1, 2, 3, 4, 5];
$arrayIterator = new ArrayIterator($array);
echo $arrayIterator->count(); // 5
또한, IteratorAggregate 인터페이스를 구현한 클래스도 Iterator::count 메서드를 사용할 수 있습니다.
#hostingforum.kr
php
class MyIteratorAggregate implements IteratorAggregate
{
private $array;
public function __construct(array $array)
{
$this->array = $array;
}
public function getIterator()
{
return new ArrayIterator($this->array);
}
}
$array = [1, 2, 3, 4, 5];
$myIteratorAggregate = new MyIteratorAggregate($array);
echo $myIteratorAggregate->getIterator()->count(); // 5
2025-03-30 22:55