如何初始化静态变量
我有这段代码:
private static $dates = array(
'start' => mktime( 0, 0, 0, 7, 30, 2009), // Start date
'end' => mktime( 0, 0, 0, 8, 2, 2009), // End date
'close' => mktime(23, 59, 59, 7, 20, 2009), // Date when registration closes
'early' => mktime( 0, 0, 0, 3, 19, 2009), // Date when early bird discount ends
);
这给了我以下错误:
解析错误:语法错误,第 19 行 /home/user/Sites/site/registration/inc/registration.class.inc 中出现意外的“(”,期望“)”
所以,我想我做错了什么......但是如果不是那样我该怎么做? 如果我用常规字符串更改 mktime 内容,它就会起作用。 所以我知道我可以这样做有点......
有人有一些指示吗?
I have this code:
private static $dates = array(
'start' => mktime( 0, 0, 0, 7, 30, 2009), // Start date
'end' => mktime( 0, 0, 0, 8, 2, 2009), // End date
'close' => mktime(23, 59, 59, 7, 20, 2009), // Date when registration closes
'early' => mktime( 0, 0, 0, 3, 19, 2009), // Date when early bird discount ends
);
Which gives me the following error:
Parse error: syntax error, unexpected '(', expecting ')' in /home/user/Sites/site/registration/inc/registration.class.inc on line 19
So, I guess I am doing something wrong... but how can I do this if not like that? If I change the mktime stuff with regular strings, it works. So I know that I can do it sort of like that..
Anyone have some pointers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
PHP 无法解析初始值设定项中的非平凡表达式。
我更喜欢通过在类定义之后添加代码来解决这个问题:
或者
PHP 5.6 可以处理现在一些表达。
PHP can't parse non-trivial expressions in initializers.
I prefer to work around this by adding code right after definition of the class:
or
PHP 5.6 can handle some expressions now.
如果您可以控制类加载,则可以从那里进行静态初始化。
示例:
在您的类加载器中,执行以下操作:
更重量级的解决方案是使用带有 ReflectionClass 的接口:
在您的类加载器中,执行以下操作:
If you have control over class loading, you can do static initializing from there.
Example:
in your class loader, do the following:
A more heavy weight solution would be to use an interface with ReflectionClass:
in your class loader, do the following:
我宁愿简单地创建一个 getter 函数,而不是寻找一种让静态变量工作的方法。 如果您需要属于特定类的数组也很有帮助,并且实现起来更简单。
无论您在哪里需要列表,只需调用 getter 方法即可。 例如:
Instead of finding a way to get static variables working, I prefer to simply create a getter function. Also helpful if you need arrays belonging to a specific class, and a lot simpler to implement.
Wherever you need the list, simply call the getter method. For example:
我结合了 Tjeerd Visser 和porneL 的答案。
但更好的解决方案是取消静态方法并使用单例模式。 然后你只需在构造函数中进行复杂的初始化即可。 或者将其作为“服务”并使用依赖注入将其注入到任何需要它的类中。
I use a combination of Tjeerd Visser's and porneL's answer.
But an even better solution is to do away with the static methods and use the Singleton pattern. Then you just do the complicated initialization in the constructor. Or make it a "service" and use dependency injection to inject it into any class that needs it.
这太复杂了,无法在定义中设置。 不过,您可以将定义设置为 null,然后在构造函数中检查它,如果它没有更改 - 设置它:
That's too complex to set in the definition. You can set the definition to null though, and then in the constructor, check it, and if it has not been changed - set it:
最好的方法是创建一个像这样的访问器:
然后你可以做 static::db(); 或 self::db(); 来自任何地方。
best way is to create an accessor like this:
then you can do static::db(); or self::db(); from anywhere.
在 PHP 7.0.1 中,我能够定义这个:
然后像这样使用它:
In PHP 7.0.1, I was able to define this:
And then use it like this:
您不能在这部分代码中进行函数调用。 如果您创建一个 init() 类型的方法,该方法在任何其他代码之前执行,那么您将能够填充该变量。
You can't make function calls in this part of the code. If you make an init() type method that gets executed before any other code does then you will be able to populate the variable then.
就我而言,我同时使用静态和非静态类属性,甚至可能在定义类之前让主程序代码引用类的静态部分。 由于类的静态部分没有构造函数,只需添加一个手动构造函数来初始化任何需要重要计算的变量:
In my case, I'm using both static and nonstatic class properties, and I might even have main program code referencing the static part of the class before defining the class. Since static portions of classes don't have constructors, just add a manual constructor to initialize any variables requiring nontrivial calculation:
这是一个代码示例中的希望有用的指针。 请注意初始化函数如何仅被调用一次。
另外,如果反转对
StaticClass::initializeStStateArr()
和$st = new StaticClass()
的调用,您将得到相同的结果。其产量:
Here is a hopefully helpful pointer, in a code example. Note how the initializer function is only called once.
Also, if you invert the calls to
StaticClass::initializeStStateArr()
and$st = new StaticClass()
you'll get the same result.Which yields :