在php中使用自定义类初始化静态成员
因为 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;
}
}
它有效,但我不太满意它。 谁能解释一下,为什么我不能执行 static $xyz = new XYZ();方式或者有更好的解决方案来解决这个问题吗?
as there are no enums in PHP I tried to do something like this:
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;
}
}
The problem is, that I get a parse error if I run the script:
Parse error: syntax error, unexpected T_NEW
I "worked it aroud" with this:
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;
}
}
It works, but I am not really happy with it.
Can anyone explain, why I can't do the static $xyz = new XYZ(); way or has a better solution for this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我知道这很烦人。我解决它就像
它有点类似于javas“静态代码块”(或任何它们所谓的^^)
该文件无论如何只能包含一次(因为发生“类已经定义”错误),所以你可以确定,该类下面的代码也被执行一次。
Its annoying, I know. I solve it like
Its a little bit similar to javas "static code blocks" (or whatever they are called ^^)
The file is only includeable once anyway (because a "class already define" error occurs), so you can be sure, that also the code below the class is executed once.
作为一种优化,您可以将对象实例存储为静态字段,这样您就不会在每次调用静态方法时都创建一个新对象:
但是,是的,您无法将新对象实例分配给类字段,这很烦人当您第一次定义该字段时。 :(
As an optimization, you could store the object instance as a static field, so that you are not creating a new object every time the static method is called:
But yes, it is annoying that you cannot assign a new object instance to a class field when you first define the field. :(
引用
static
的手册页:这就是为什么你不能这样做
Quoting the manual page of
static
:That is why you cannot do