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
Raheel
  • 8,716
  • 9
  • 60
  • 102

3 Answers3

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.

Elon Than
  • 9,603
  • 4
  • 27
  • 37
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

Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
  • So by your example i think it is same case for non static properties ? We will have same problem. – Raheel Sep 15 '15 at 17:07
  • 1
    @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` – Elias Van Ootegem Sep 16 '15 at 09:06
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();
Community
  • 1
  • 1
Halayem Anis
  • 7,654
  • 2
  • 25
  • 45