2
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 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

@RaheelKhan oh, I missed that you want to assign that to another static property, not just use. Check edited answer.
Oh damn, I would probably need to use getters for this :(
1

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

2 Comments

So by your example i think it is same case for non static properties ? We will have same problem.
@RaheelKhan: Of course, what's more: with non-static properties, you can't initialize/declare them using public $foo = $this->bar; because $this doesn't exist before the instance is created. You'll have to use the constructor to initialize the properties using $this
1

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

1 Comment

Thanks for the nice solution.

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.