douci7521
douci7521
2015-09-15 16:47

在类中访问静态数组时出现语法错误

已采纳
public static $config = array(
    'base_url' => '',
    'environment' => '',
    'database' => array(
        'dbdriver' => '',
        'dbhost'   => '',
        'dbname'   => '',
        'dbuser'   => '',
        'dbpass'   => ''
    ),

I want to access the base_url key and assign it to a new static property $app but it is giving me syntax error of unexpected [

public static $app_path = self::config['base_url']; //unexpected [ error
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • douluan8828 douluan8828 6年前

    You want to access variable so you have to add $.

    self::$config['base_url']
    

    Read some more about that here.

    Unfortunately you can't assign any variable (even static) to other static property as you can see in linked manual page.

    Like any other PHP static variable, static properties may only be initialized using a literal or constant; expressions are not allowed. So while you may initialize a static property to an integer or array (for instance), you may not initialize it to another variable, to a function return value, or to an object.

    点赞 9 评论 复制链接分享
  • douhao2721 douhao2721 6年前

    The way you've initilized your (static) properties, is not yet implemented in PHP
    You can check this thread's explanation: https://stackoverflow.com/a/1633024/4098311

    However, this is how i'm doing in my projects :

    <?php
    class YourClass {
        public static $config       = NULL;
        public static $app_path     = NULL;
        public static $_INITIALIZED = FALSE;
    
        public static init() {
            if(!self::$_INITIALIZED) {
                self::$config = array(
                                    'base_url' => '',
                                    'environment' => '',
                                    'database' => array(
                                        'dbdriver' => '',
                                        'dbhost'   => '',
                                        'dbname'   => '',
                                        'dbuser'   => '',
                                        'dbpass'   => ''
                                    ));
                self::$app_path = self::config['base_url'];
                self::$_INITIALIZED = TRUE;
            }
        }
        // ....
        // Your Stuf ...
        // ....
    }
    YourClass::init();
    
    点赞 7 评论 复制链接分享
  • dopgl80062 dopgl80062 6年前

    Read the manual:

    Like any other PHP static variable, static properties may only be initialized using a literal or constant; expressions are not allowed. So while you may initialize a static property to an integer or array (for instance), you may not initialize it to another variable, to a function return value, or to an object.

    Right here

    And before you think this is a severe limitation, let me tell you why this is a blessed relief:

    class Foo
    {
        public static $evil = array('bar' => 123);
        public static $check = self::$evil['bar'];
    }
    Foo::$check;//all is well
    

    But then, when you introduce late static binding (something we all love):

    class Foo
    {
        public static $evil = array('bar' => 123);
        public static $check = static::$evil['bar'];
    }
    class Bar extends Foo
    {
        public static $evil = 123;
    }
    Bar::$check;//OOOPS
    

    TL;TR: statics are something like super-globals: you can initialize them with a constant expression, but they can't require state to be initialized, that would be inception madness

    点赞 6 评论 复制链接分享

相关推荐