라이브러리
[PHP] Iterator::current - 현재 요소를 반환합니다.
Iterator::current() 메서드
Iterator::current() 메서드는 현재 반복자에 의해 반복 중인 요소의 값을 반환합니다. 이 메서드는 반복자에 의해 반복 중인 요소의 현재 위치를 반환합니다.
사용 예제
#hostingforum.kr
php
// 예제 1: ArrayIterator 사용
$array = array(1, 2, 3, 4, 5);
$iterator = new ArrayIterator($array);
echo $iterator->current(); // 1
$iterator->next();
echo $iterator->current(); // 2
#hostingforum.kr
php
// 예제 2: RecursiveIteratorIterator 사용
$dir = new RecursiveDirectoryIterator('.');
$iterator = new RecursiveIteratorIterator($dir);
echo $iterator->current()->getFilename(); // 현재 디렉토리의 파일 이름
#hostingforum.kr
php
// 예제 3: 사용자 정의 반복자 사용
class MyIterator implements Iterator {
private $data = array(1, 2, 3, 4, 5);
private $position = 0;
public function rewind() {
$this->position = 0;
}
public function current() {
return $this->data[$this->position];
}
public function key() {
return $this->position;
}
public function next() {
$this->position++;
}
public function valid() {
return isset($this->data[$this->position]);
}
}
$iterator = new MyIterator();
echo $iterator->current(); // 1
$iterator->next();
echo $iterator->current(); // 2
Iterator::current() 메서드의 특징
- 반복자에 의해 반복 중인 요소의 현재 위치를 반환합니다.
- 반복자에 의해 반복 중인 요소의 값을 반환합니다.
- 반복자에 의해 반복 중인 요소의 위치를 변경하지 않습니다.
Iterator::current() 메서드의 사용 예시
- 반복자에 의해 반복 중인 요소의 값을 반환할 때 사용합니다.
- 반복자에 의해 반복 중인 요소의 현재 위치를 반환할 때 사용합니다.
Iterator::current() 메서드의 제한 사항
- 반복자에 의해 반복 중인 요소가 없을 때 사용할 수 없습니다.
- 반복자에 의해 반복 중인 요소의 위치를 변경할 수 없습니다.
-
- 나우호스팅 @pcs8404
-
호스팅포럼 화이팅!
댓글목록
등록된 댓글이 없습니다.