라이브러리
[PHP] NoRewindIterator::next - 다음 요소로 전달
NoRewindIterator::next()
PHP의 `NoRewindIterator`는 내부적으로 `Iterator` 인터페이스를 구현한 클래스입니다. `NoRewindIterator`는 `Iterator`의 `rewind()` 메서드를 호출하지 않습니다. 이 클래스는 `Iterator`의 `next()` 메서드를 호출할 때 사용됩니다.
`NoRewindIterator::next()` 메서드는 현재 위치를 다음 위치로 이동시키는 메서드입니다. 이 메서드는 `Iterator` 인터페이스의 `next()` 메서드와 동일하게 작동합니다.
예제
#hostingforum.kr
php
// 예제 1: NoRewindIterator를 사용하여 배열을 반복하는 예제
$array = [1, 2, 3, 4, 5];
$iterator = new ArrayIterator($array);
$noRewindIterator = new NoRewindIterator($iterator);
echo $noRewindIterator->current() . "
"; // 1
$noRewindIterator->next();
echo $noRewindIterator->current() . "
"; // 2
$noRewindIterator->next();
echo $noRewindIterator->current() . "
"; // 3
예제 2: NoRewindIterator를 사용하여 객체를 반복하는 예제
#hostingforum.kr
php
// 예제 2: NoRewindIterator를 사용하여 객체를 반복하는 예제
class Person {
public $name;
public $age;
public function __construct($name, $age) {
$this->name = $name;
$this->age = $age;
}
}
$people = [
new Person('John', 25),
new Person('Jane', 30),
new Person('Bob', 35),
];
$iterator = new ArrayIterator($people);
$noRewindIterator = new NoRewindIterator($iterator);
echo $noRewindIterator->current()->name . "
"; // John
$noRewindIterator->next();
echo $noRewindIterator->current()->name . "
"; // Jane
$noRewindIterator->next();
echo $noRewindIterator->current()->name . "
"; // Bob
예제 3: NoRewindIterator를 사용하여 파일을 반복하는 예제
#hostingforum.kr
php
// 예제 3: NoRewindIterator를 사용하여 파일을 반복하는 예제
$file = fopen('example.txt', 'r');
$iterator = new SplFileObject($file);
$noRewindIterator = new NoRewindIterator($iterator);
echo $noRewindIterator->current() . "
"; // 파일의 첫 번째 줄
$noRewindIterator->next();
echo $noRewindIterator->current() . "
"; // 파일의 두 번째 줄
$noRewindIterator->next();
echo $noRewindIterator->current() . "
"; // 파일의 세 번째 줄
위 예제에서 `NoRewindIterator`를 사용하여 배열, 객체, 파일을 반복하는 방법을 보여줍니다. `NoRewindIterator::next()` 메서드는 현재 위치를 다음 위치로 이동시키는 메서드입니다.
-
- 나우호스팅 @pcs8404
-
호스팅포럼 화이팅!
댓글목록
등록된 댓글이 없습니다.