Search
 
SCRIPT & CODE EXAMPLE
 

PHP

iterator impliment php


<?php
class myIterator implements Iterator {
    private $position = 0;
    private $array = array(
        "firstelement",
        "secondelement",
        "lastelement",
    );  

    public function __construct() {
        $this->position = 0;
    }

    public function rewind() {
        var_dump(__METHOD__);
        $this->position = 0;
    }

    public function current() {
        var_dump(__METHOD__);
        return $this->array[$this->position];
    }

    public function key() {
        var_dump(__METHOD__);
        return $this->position;
    }

    public function next() {
        var_dump(__METHOD__);
        ++$this->position;
    }

    public function valid() {
        var_dump(__METHOD__);
        return isset($this->array[$this->position]);
    }
}

$it = new myIterator;

foreach($it as $key => $value) {
    var_dump($key, $value);
    echo "
";
}
?>

  
  
  // output
  
string(18) "myIterator::rewind"
string(17) "myIterator::valid"
string(19) "myIterator::current"
string(15) "myIterator::key"
int(0)
string(12) "firstelement"

string(16) "myIterator::next"
string(17) "myIterator::valid"
string(19) "myIterator::current"
string(15) "myIterator::key"
int(1)
string(13) "secondelement"

string(16) "myIterator::next"
string(17) "myIterator::valid"
string(19) "myIterator::current"
string(15) "myIterator::key"
int(2)
string(11) "lastelement"

string(16) "myIterator::next"
string(17) "myIterator::valid"

Comment

PREVIOUS NEXT
Code Example
Php :: php parse xml 
Php :: php delete item from array 
Php :: laravel display validation errors ajax 
Php :: how to hide get parameters in url php 
Php :: php show error 
Php :: laravel form submit page expired 
Php :: wordpress get post taxonomy terms 
Php :: laravel custom error page 
Php :: CORSS oauth/token lavarel 
Php :: how to get the last inserted id in laravel 
Php :: how to run a specific test in laravel 
Php :: replace all php 
Php :: please provide a valid cache path. laravel 
Php :: php 8 major changes - Nullsafe operator 
Php :: laravel eloquent sum column 
Php :: laravel get class name 
Php :: symfony get api paths 
Php :: get session id in laravel 
Php :: xdebug vscode docker 
Php :: php convert date string to number 
Php :: php mb_convert_case 
Php :: remove double space php 
Php :: yii2 get action class in view 
Php :: current loggedin user laravel 
Php :: operador in laravel 
Php :: convert json object to array in php 
Php :: storePublicly laravel with name 
Php :: how to print count query in php 
Php :: csv to array in php 
Php :: carbon add few hours 
ADD CONTENT
Topic
Content
Source link
Name
9+4 =