如何在数组中声明静态数组?

时间:2017-08-22 11:43:06

标签: php

我的代码:

class User
    {
        protected static $config = array(
            'expiration'    => 0,       
        );

        protected static $Db;

        protected static $user = array();
                  static::$user['data'] = array();
                  static::$user['meta'] = array();
                  static::$user['controls'] = array();

我将在此处收到此错误:

Parse error: syntax error, unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM), expecting variable (T_VARIABLE)

抱怨这个:

`static::$user['data'] = array()`

如何在这里声明static :: $ user ['data']?

2 个答案:

答案 0 :(得分:1)

您已将$user定义为静态,因此您无需再次将其元素声明为静态。如果要初始化它。您可以通过以下方法实现它:

class User
    {
        protected static $config = array(
            'expiration'    => 0,       
        );

        protected static $Db;

        protected static $user = array();        
        public static function get_user()
        {
          self::$user['data'] = array();
          self::$user['meta'] = array();
          self::$user['controls'] = array();
          return self::$user;
        }

}
$user = User::get_user();
var_dump($user);

答案 1 :(得分:1)

静态数组变量不需要显式地使它的key =>值也是静态的,因为它会自动处理。

你需要这样做: -

self::$user['data'] = array();
self::$user['meta'] = array();
self::$user['controls'] = array();