Skip to content

Instantly share code, notes, and snippets.

@stvkoch
Last active August 29, 2015 23:15
Show Gist options
  • Save stvkoch/58636 to your computer and use it in GitHub Desktop.
Save stvkoch/58636 to your computer and use it in GitHub Desktop.
<?php
/**
* Cycle::run('tabledados', 'red', 'blue'); //red
* Cycle::run('tabledados', 'red', 'blue'); //blue
* Cycle::run('tabledados', 'red', 'blue'); //red
* Cycle::run('tabledados', 'red', 'blue'); //blue
*/
class Cycle{
protected $values;
protected $ref;
protected static $inst=array();
protected function __construct($values){
$this->values = $values;
$this->ref = 0;
return $this;
}
public function run(){
$values = func_get_args();
$index = array_shift($values);
if(!isset(self::$inst[$index]))
self::$inst[$index] = new Cycle($values );
return self::$inst[$index]->set_cycle();
}
protected function set_cycle(){
if($this->ref+1 < (count($this->values)) ){
$this->ref = $this->ref + 1;
}else{
$this->ref = 0;
}
return $this->values[$this->ref];
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment