如何初始化静态变量 [英] How to initialize static variables

查看:164
本文介绍了如何初始化静态变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码:

  private static $ dates = array(
'start'=> mktime (0,0,0,7,30,2009),//开始日期
'end'=> mktime(0,0,0,8,2,2009),//结束日期
'close'=> mktime(23,59,59,7,20,2009),//注册关闭时的日期
'early'=> mktime(0,0,0,3,19, 2009),//早鸟折扣结束日期
);

这会给我以下错误:



< >

解析错误:语法错误,在第19行的/home/user/Sites/site/registration/inc/registration.class.inc中出现意外的'(',expecting')'


所以,我想我做错了...但是如果不是这样,我该怎么办呢?如果我改变mktime东西与常规字符串,它的工作原理。所以我知道我可以这样做。



任何人都有一些指针?



我喜欢解决这个问题,因为我不能解决这个问题。在类的定义后添加代码:

  class Foo {
static $ bar;
}
Foo :: $ bar = array(...);

 c $ c> class Foo {
private static $ bar;
static function init()
{
self :: $ bar = array(...);
}
}
Foo :: init();






PHP 5.6 现在可以处理一些表达式。


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?

解决方案

PHP can't parse non-trivial expressions in initializers.

I prefer to work around this by adding code right after definition of the class:

class Foo {
  static $bar;
}
Foo::$bar = array(…);

or

class Foo {
  private static $bar;
  static function init()
  {
    self::$bar = array(…);
  }
}
Foo::init();


PHP 5.6 can handle some expressions now.

这篇关于如何初始化静态变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆