tags:

views:

42

answers:

2

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"]=>
+1  A: 

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.

watain
+3  A: 

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.

zombat
Why does it show all the values when I var_dump the entire thing but not when I try to var_dump the specific value? If privacy was the issue, why does it sometimes get outputted?
Citizen
`var_dump()` specifically shows everything about a variable, including protected and private data. However, that doesn't mean you can access the protected and private data in the code. For instance, if you have an object `$obj` and you call `var_dump($obj)`, you will see the entire object. However, if you try to `var_dump($obj->privateMember)`, you won't get anything, because your code doesn't have access to `$obj->privateMember`, and it can't be passed to `var_dump()`.
zombat
I'll post an example...
zombat