0

I am beginner in php

As i know Iterator is an interface to create iterator of a class and has 5 abstract methods. But i dont know when these methods are called when these methods perform their duty in foreach loop, what these methods do ?

All internet is inspected fot those 5 methods including php.net, sitepoint, etc.

Can anyone tell what does these methods do in Iterator interface in foreach loop?

Please explain it with the following piece of code as example.

class myClass implements Iterator{

    public function rewind(){
        //--What it will do ?
    }

    public function current(){
        //--What it will do ?
    }

    public function key(){
        //--What it will do ?
    }

    public function next(){
        //--What it will do ?
    }

    public function valid(){
        //--What it will do ?
    }

}

$obj = new myClass();

foreach($obj as $value){

    //--What is the duty or realtionship of rewind(), current(), key(), next(), valid() methods here in foreach loop ?

}

Can anyone tell in simple and easy words ?

  • 3
    http://php.net/manual/en/class.iterator.php#iterator.examples – u_mulder Sep 29 '17 at 16:37
  • I am already on that page which does not provide information that is able to understand i am beginner. –  Sep 29 '17 at 16:39
  • 1
    The top comment on the page linked to by @u_mulder is actually an exact answer to your question: http://php.net/manual/en/class.iterator.php#96691 -- that explains what each function does and in what order they are called during a `foreach` – rickdenhaan Sep 29 '17 at 16:39
  • So how they are called in foreach what does it mean that they are called in foreach, what exactly it means ? –  Sep 29 '17 at 16:40
  • It means `foreach($obj as $key => $value)` does exactly the same as the example while-loop in that comment (just replace `$it` with `$obj` in their code) – rickdenhaan Sep 29 '17 at 16:41

0 Answers0