
ArrayIterator::current 메서드는 현재 반복자에 의해 참조되는 요소를 반환합니다.
반복자에 의해 참조되는 요소는 반복자가 현재 위치한 요소를 의미합니다.
반복자에서 next 메서드를 호출하면 현재 위치가 다음 요소로 이동되며, current 메서드는 새로운 현재 위치에 대한 값을 반환합니다.
예를 들어,
#hostingforum.kr
php
$array = [1, 2, 3];
$iterator = new ArrayIterator($array);
echo $iterator->current(); // 1
$iterator->next();
echo $iterator->current(); // 2
위 코드에서 current 메서드는 반복자가 현재 위치한 요소를 반환합니다.
반복자에서 rewind 메서드를 호출하면 현재 위치가 처음 요소로 이동되며, current 메서드는 처음 요소의 값을 반환합니다.
예를 들어,
#hostingforum.kr
php
$array = [1, 2, 3];
$iterator = new ArrayIterator($array);
$iterator->next();
echo $iterator->current(); // 2
$iterator->rewind();
echo $iterator->current(); // 1
위 코드에서 current 메서드는 반복자가 현재 위치한 요소를 반환합니다.
반복자가 끝에 도달하면 current 메서드는 Notice: ArrayIterator::current(): Current value cannot be retrieved. 오류를 발생시킵니다.
예를 들어,
#hostingforum.kr
php
$array = [1, 2, 3];
$iterator = new ArrayIterator($array);
$iterator->next();
$iterator->next();
$iterator->next();
echo $iterator->current(); // Notice: ArrayIterator::current(): Current value cannot be retrieved.
위 코드에서 current 메서드는 오류를 발생시킵니다.
따라서, ArrayIterator::current 메서드는 반복자가 현재 위치한 요소를 반환하며, 반복자가 끝에 도달하면 오류를 발생시킵니다.
2025-05-09 14:19