1

My code:

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();

Here I will get this error:

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

by complaining this:

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

How can I declare static::$user['data'] here?

2
  • 4
    You can't declare a static variable inside an array. The initial array will be static so you wouldn't need to make its key values static anyway. Commented Aug 22, 2017 at 11:46
  • 2
    Somewhere in a class method: self::$user['key'] = array() Commented Aug 22, 2017 at 11:49

2 Answers 2

1

A static array variable don't need to explicitly make it's key=>value static too, because it will be taken care automatically.

You need to do like this:-

self::$user['data'] = array();
self::$user['meta'] = array();
self::$user['controls'] = array();
Sign up to request clarification or add additional context in comments.

Comments

1

You already have define $user as static so you don't need to declare its elements as static again. If you want to initialize it. You can achieve it by method like below:

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);

2 Comments

Slightly confusing, as every time you get_user, you'll be clobbering User::$user.
Its just an example. initialization can be done in other function also.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.