// Verwendungsbeispiel
$states = array('A', 'B', 'C');
$osz = new oszillation($states);
for($i = 10; $i; $i--) {
echo $osz->oszillate()." $i \n";
}
/*
Creates an object that helps doing oszillating things, such as table rows with alternating colours.
Bugs/known issues: if the absolute value of parameter $by of oszillate() is greater than number_of_states the function won't work (producing notices over and over)
*/
class oszillation {
var $states;
var $currentstate; // The pointer
var $number_of_states;
/*
void __construct(array $values)
array $values: Values contain the different strings that should take turns. The array will be reindexed.
*/
function __construct($values) {
$this->currentstate = 0;
$this->states = array();
foreach($values as $single_state) { // Reindex
$this->states[] = $single_state;
}
$this->number_of_states = count($this->states);
echo "States: ".$this->number_of_states." \n\n";
}
function oszillation($values){$this->__construct($values);} // PHP4
/*
mixed oszillate([int $by])
signed int $by (Default 1): Determines how far the internal pointer is pushed forward after (!) the oszillation
Returns the current state of the oszillation and sets the internal pointer to a new location (which is then the current state given back upon next use of the function).
Returns the value the pointer points to.
*/
function oszillate($by = 1) {
$return = $this->states[$this->currentstate];
$this->currentstate += (int)$by;
if($this->currentstate > $this->number_of_states - 1 OR $this->currentstate < 0) { // Range exceeded
$this->currentstate = $this->currentstate - floor($this->currentstate / $this->number_of_states) * $this->number_of_states; // Mathematical modulo
}
return $return;
}
}