在JAVA中有如下代码:
1 2 3 4 5 6 7 8 9 10 |
public class Test { static { _i = 10; } public static int _i; public static void main(String[] args) { System.out.println(_i); } } |
PHP中没有static块儿的概念,可以如下实现:
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php class Test{ private static $init_ok = 0; private static $data; public static function class_init() { if (!self::$init_ok) { self::$data = array(/*.... */); } } } Test::class_init(); |
参考资料: http://blog.csdn.net/darxin/article/details/5293427