
CachingIterator의 key() 메서드는 현재 위치에 있는 키를 반환합니다. 그러나 만약 현재 위치가 캐싱되지 않은 데이터일 경우, key() 메서드는 null을 반환합니다.
위의 예시를 통해 설명해 드리겠습니다.
#hostingforum.kr
php
$cachingIterator = new CachingIterator(new ArrayIterator(['a', 'b', 'c']));
print_r($cachingIterator->key()); // 결과: 0
$cachingIterator->next();
$cachingIterator->next();
print_r($cachingIterator->key()); // 결과: 2
$cachingIterator->next(); // 현재 위치가 캐싱되지 않은 데이터일 경우
print_r($cachingIterator->key()); // 결과: null
위의 예시에서, $cachingIterator->next()를 호출하여 현재 위치가 캐싱되지 않은 데이터인 'c'를 넘어갈 때, key() 메서드는 null을 반환합니다.
2025-08-16 05:21