在php中使用自定义类初始化静态成员

发布于 2024-10-31 11:46:54 字数 1288 浏览 0 评论 0原文

因为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

复古式 2024-11-07 11:46:54

我知道这很烦人。我解决它就像

class Foo {
  public static $var;
}
Foo::$var = new BarClass;

它有点类似于javas“静态代码块”(或任何它们所谓的^^)

该文件无论如何只能包含一次(因为发生“类已经定义”错误),所以你可以确定,该类下面的代码也被执行一次。

Its annoying, I know. I solve it like

class Foo {
  public static $var;
}
Foo::$var = new BarClass;

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.

苍风燃霜 2024-11-07 11:46:54

作为一种优化,您可以将对象实例存储为静态字段,这样您就不会在每次调用静态方法时都创建一个新对象:

private static $noCache;
public static function NO_CACHE(){
  if (self::$noCache == null){
    self::$noCache = new CacheMode(1, __("No cache",'footballStandings'));
  }
  return self::$noCache;
}

但是,是的,您无法将新对象实例分配给类字段,这很烦人当您第一次定义该字段时。 :(

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:

private static $noCache;
public static function NO_CACHE(){
  if (self::$noCache == null){
    self::$noCache = new CacheMode(1, __("No cache",'footballStandings'));
  }
  return self::$noCache;
}

But yes, it is annoying that you cannot assign a new object instance to a class field when you first define the field. :(

空心空情空意 2024-11-07 11:46:54

引用 static 的手册页

与任何其他 PHP 静态变量一样,
静态属性只能是
使用文字或初始化
持续的;不允许使用表达式。
因此,虽然您可以初始化静态
属性为整数或数组(例如
实例),你不能初始化它
到另一个变量,到一个函数
返回值,或一个对象。

这就是为什么你不能这样做

public static $NO_CACHE = new CacheMode(1, "No cache");

Quoting the manual page of static :

Like any other PHP static variable,
static properties may only be
initialized using a literal or
constant; expressions are not allowed.
So while you may initialize a static
property to an integer or array (for
instance), you may not initialize it
to another variable, to a function
return value, or to an object.

That is why you cannot do

public static $NO_CACHE = new CacheMode(1, "No cache");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文