<?phpclass MyIterator implements Iterator{private $var = array();public function __construct ( $array ){if ( is_array ( $array )){$this -> var = $array ;}}//返回到迭代器的第一个元素public function rewind (){echo "rewinding返回到迭代器的第一个元素\n" ;reset ( $this -> var );}//返回当前元素public function current (){$var = current ( $this -> var );echo "current返回当前元素: $var \n" ;return $var ;}//返回当前元素的键public function key (){$var = key ( $this -> var );echo "key返回当前元素的键: $var \n" ;return $var ;}//向前移动到下一个元素public function next (){$var = next ( $this -> var );echo "next向前移动到下一个元素: $var \n" ;return $var ;}//检查当前位置是否有效public function valid (){$var = $this -> current () !== false ;echo "valid检查当前位置是否有效: { $var } \n" ;return $var ;}}$values = array( 1 , 2 , 3 );$it = new MyIterator ( $values );foreach ( $it as $a => $b ) {print " $a : $b \n" ;}?>
第一句子网 - 唯美句子、句子迷、好句子大全