라이브러리

[PHP] ParentIterator::rewind - 반복자를 되감습니다.




ParentIterator::rewind


PHP의 Iterator 인터페이스는 데이터를 순회할 수 있는 객체를 제공합니다. ParentIterator는 이 인터페이스를 구현한 클래스 중 하나로, 부모 클래스의 Iterator를 상속받아 사용할 수 있습니다.

ParentIterator::rewind는 부모 클래스의 Iterator를 초기화하는 메서드입니다. 이 메서드는 부모 클래스의 Iterator를 처음부터 다시 시작하도록 합니다.

예제


다음 예제는 ParentIterator::rewind를 사용하는 방법을 보여줍니다.

#hostingforum.kr
php

class ParentIterator implements Iterator {

    private $data;

    private $position;



    public function __construct($data) {

        $this->data = $data;

        $this->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 $this->position < count($this->data);

    }

}



class ChildIterator extends ParentIterator {

    public function rewind() {

        parent::rewind(); // 부모 클래스의 rewind를 호출합니다.

        echo "ChildIterator의 rewind가 호출되었습니다.
";

    }

}



$data = array(1, 2, 3, 4, 5);

$childIterator = new ChildIterator($data);



// ChildIterator의 rewind를 호출합니다.

$childIterator->rewind();



// Iterator를 순회합니다.

while ($childIterator->valid()) {

    echo $childIterator->current() . "
";

    $childIterator->next();

}



이 예제에서 ChildIterator는 ParentIterator를 상속받고, ParentIterator의 Iterator를 초기화하는 메서드인 rewind를 호출합니다. ChildIterator의 rewind를 호출하면 부모 클래스의 rewind가 호출되고, Iterator를 처음부터 다시 시작합니다.

결과


#hostingforum.kr


ChildIterator의 rewind가 호출되었습니다.

1

2

3

4

5



결론


ParentIterator::rewind는 부모 클래스의 Iterator를 초기화하는 메서드입니다. 이 메서드는 부모 클래스의 Iterator를 처음부터 다시 시작하도록 합니다. ChildIterator에서 ParentIterator::rewind를 호출하면 부모 클래스의 Iterator를 초기화하고, Iterator를 처음부터 다시 시작합니다.
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

  • 전체 10,077건 / 345 페이지

검색

게시물 검색