TIPO RANGE
Permite manejar un tipo de dato rango que ademas es iterable (implementa Iterator):
Código PHP:
$rango = new Range(1,100,2);
foreach ($rango as $valor){
echo "$valor\n";
}
echo $rango;
echo str_replace (' ',' ,',$rango);
Quien lo implementa:
Código PHP:
<?php
/*
Clase Range
@author: Pablo Bozzolo (italico76)
*/
Class Range implements Iterator {
private $_arr = [];
private $_ini = null;
private $_end = null;
private $_step = null;
public function __construct($a,$b,$step=null){
$this->_ini = $a;
$this->_end = $b;
$this->_step = ($step == null ? 1 : $step);
$this->_arr = range($this->_ini,$this->_end,$this->_step);
}
public function getArray(){
return $this->_arr;
}
public function __toString(){
$out = null;
foreach ($this->_arr as $val)
$out .= "$val ";
return rtrim($out);
}
/* Implemento interface Iterator */
public function rewind()
{
reset($this->_arr);
}
public function current()
{
$elem = current($this->_arr);
return $elem;
}
public function next()
{
$elem = next($this->_arr);
return $elem;
}
public function key()
{
$key = key($this->_arr);
return $key;
}
public function valid()
{
$key = key($this->_arr);
$elem = ($key !== NULL && $key !== FALSE);
return $elem;
}
} // endClass