Parris Varney
Parris Varney

Reputation: 11478

Accessing a private static member

Not sure if this stopped working due to a recent upgrade to PHP5.6 or what.

namespace Data;

class AWS{
    private static $config;

    public static function setup($config){
        if(isset(self::$config)){
            throw new Exception("AWS has already been setup.");
        }
        self::$config = $config;
    }

    ...
}

Then from a different file:

use \Data\AWS;
AWS::setup($array_of_configs);

Calling setup gives:

Fatal error: Access to undeclared static property: CoPatient\Data\AWS::$config in /var/www/html/src/data/AWS.php on line 24

Using xdebug I can confirm that $config contains an 1-d associative array.

Edit: This only seems to be happening if I've got an xdebug listener running.

Upvotes: 0

Views: 103

Answers (2)

acupofjose
acupofjose

Reputation: 2159

I believe you're just accessing it wrong when calling the method. Probably using an instance selector like: $a = new AWS(); $a->setup();

class AWS {
    private static $config;

    public static function setup($config){
        if(isset(self::$config)){
            throw new Exception("AWS has already been setup.");
        }
        var_dump(self::$config);
        self::$config = $config;
        var_dump(self::$config);
    }

    public static function getConfig() {
       return self::$config;   
    }
}

AWS::setup(array('test'));
var_dump(AWS::getConfig());

Should give an output of:

NULL
array(1) {
  [0]=>
  string(4) "test"
}
array(1) {
  [0]=>
  string(4) "test"
}

Fiddle: http://www.tehplayground.com/#idd0F1WGk

Upvotes: 2

gbu
gbu

Reputation: 56

I think You just don't do $aws = new AWS() before call setup. That's right ?

Upvotes: 0

Related Questions