我有这门课,
class Player {
protected static $volume;
protected static $instance = null;
protected function __construct()
{
self::$volume = 5;
$this->maxVolume = 20;
}
protected function __clone() {}
public static function getInstance() {
if (!isset(static::$instance)) {
static::$instance = new static;
}
return static::$instance;
}
public function volumeUp() {
if (self::$volume < $this->maxVolume) {
self::$volume = self::$volume + 1;
}
return static::$volume;
}
public function volumeDown () {
if (self::$volume > 0) {
self::$volume = self::$volume - 1;
}
return static::$volume;
}
}
我想通过调用volumeUp或volumeDown方法动态更改对象值。
$player = \Player\Player::getInstance();
if (isset($data['volume']) && isset($data['down'])) {
echo $player->volumeDown();
}
if (isset($data['volume']) && isset($data['up'])) {
echo $player->volumeUp();
}
当我调用那些方法时,对象就像新实例一样,并且给我总是相同的结果。
我做错了什么?