Citizen
Citizen

Reputation: 12947

How would I access this object value?

I'm trying to echo and access the values stored in ["_aVars:private"]

$obj->_vars and $obj->_vars:private doesnt work :(

Here's the var_dump of $obj

object(test_object)#15 (30) {
  ["sDisplayLayout"]=>
  string(8) "template"
  ["bIsSample"]=>
  bool(false)
  ["iThemeId"]=>
  int(0)
  ["sReservedVarname:protected"]=>
  string(6) "test"
  ["sLeftDelim:protected"]=>
  string(1) "{"
  ["sRightDelim:protected"]=>
  string(1) "}"
  ["_aPlugins:protected"]=>
  array(0) {
  }
  ["_aSections:private"]=>
  array(0) {
  }
  ["_aVars:private"]=>
  array(56) {
    ["bUseFullSite"]=>
    bool(false)
    ["aFilters"]=>

Upvotes: 0

Views: 964

Answers (2)

zombat
zombat

Reputation: 94167

The :private part of the var_dump output isn't actually part of the member name, it's an indicator that the _aVars member is private.

Because _aVars is private, it's value cannot be accessed from outside of the object, only from inside.

You'd need a public getter function or something similar in order to retrieve the value.

Edit

To help clarify this, I made an example:

class testClass {
    public $x = 10;
    private $y = 0;
}

$obj = new testClass();
echo "Object: ";
var_dump($obj);
echo "Public property:";
var_dump($obj->x);
echo "Private property:";
var_dump($obj->y);

The above code produces the following output:

Object:

object(testClass)[1]
  public 'x' => int 10
  private 'y' => int 0

Public property:

int 10

Private property:

Notice how nothing comes after the attempted var_dump() of the private variable. Since the code doesn't have access to $obj->y from outside, that means that var_dump() cannot access it to produce any information about it.

Since $obj is a local variable however, var_dump() works fine there. It's a specific characteristic of var_dump() that it will output information about protected and private object member variables, so that's why you see it in the object dump. It doesn't mean that you have access to them however.

Upvotes: 4

watain
watain

Reputation: 5098

You can't access it because it's a private method :). I don't think there's a way to access it at all, as long as you don't change it to public.

Upvotes: 1

Related Questions