【发布时间】:2011-04-09 15:47:51
【问题描述】:
因为 PHP 中没有枚举,所以我尝试做这样的事情:
class CacheMode{
public static $NO_CACHE = new CacheMode(1, "No cache");
private $id, $title;
public function getId(){
return $this->id;
}
public function getTitle(){
return $this->title;
}
private function __construct($id, $title){
$this->id = $id;
$this->title = $title;
}
}
问题是,如果我运行脚本会出现解析错误:
Parse error: syntax error, unexpected T_NEW
我用这个“努力工作”:
class CacheMode{
public static function NO_CACHE(){
return new CacheMode(1, __("No cache",'footballStandings'));
}
public static function FILE_CACHE(){
return new CacheMode(2, __("Filecache",'footballStandings'));
}
public static function VALUES(){
return array(self::NO_CACHE(), self::FILE_CACHE());
}
private $id, $title;
public function getId(){
return $this->id;
}
public function getTitle(){
return $this->title;
}
private function __construct($id, $title){
$this->id = $id;
$this->title = $title;
}
}
它有效,但我对它并不满意。 谁能解释一下,为什么我不能做静态 $xyz = new XYZ();有没有更好的办法解决这个问题?
【问题讨论】:
标签: php initialization static-members