hi,
<?php
class Colors
{
private $_serFileName = "myColorsShuffled.ser";
private $_colors = array("blau", "rot", "grün", "schwarz");
private $_colorsShuffled = array();
private $_nextColor = "";
public function __construct() {
if(!file_exists($this->_serFileName)) {
$this->_createNewShuffledFile();
}
$this->_read();
}
private function _createNewShuffledFile() {
$this->_colorsShuffled = $this->_colors;
shuffle($this->_colorsShuffled);
$this->_write();
}
private function _write() {
file_put_contents($this->_serFileName, serialize($this->_colorsShuffled));
}
private function _read() {
$this->_colorsShuffled = unserialize(file_get_contents($this->_serFileName));
}
public function getNext() {
//~ var_dump($this->_colorsShuffled);
if (count($this->_colorsShuffled) === 0) {
$this->_createNewShuffledFile();
}
$actualColor = array_shift($this->_colorsShuffled);
$this->_write();
return $actualColor;
}
};
$myColors = new Colors();
echo $myColors->getNext();
Sollte "funzen". ggfs. mal das Var_dump() wieder einkommentieren ...;
mfg
tami