
IteratorIterator::current 함수는 현재 반복자에 있는 값을 얻기 위해 사용됩니다. 이 함수는 반복자에 있는 값을 수정할 수 없습니다.
반복자에 있는 값을 얻기 위해 IteratorIterator::current 함수를 사용하는 예제는 다음과 같습니다.
#hostingforum.kr
php
$iterator = new ArrayIterator(array(1, 2, 3));
$current = $iterator->current();
print($current); // 1
IteratorIterator::current 함수를 사용할 때 예외가 발생할 수 있습니다. 예를 들어, 반복자에 있는 값이 없을 때 IteratorIterator::current 함수를 호출하면 Exception이 발생합니다.
#hostingforum.kr
php
$iterator = new ArrayIterator(array());
try {
$iterator->current();
} catch (Exception $e) {
print($e->getMessage());
}
IteratorIterator::current 함수를 사용할 때 주의해야 하는 점은 반복자에 있는 값을 얻기 전에 반복자를 rewind() 함수로 초기화해야 한다는 것입니다.
#hostingforum.kr
php
$iterator = new ArrayIterator(array(1, 2, 3));
$iterator->rewind();
$current = $iterator->current();
print($current); // 1
또한, 반복자에 있는 값을 얻기 전에 반복자에 있는 값을 next() 함수로 이동해야 한다는 점도 주의해야 합니다.
#hostingforum.kr
php
$iterator = new ArrayIterator(array(1, 2, 3));
$iterator->next();
$current = $iterator->current();
print($current); // 2
2025-08-13 06:10