How would I access this object value?

别说谁变了你拦得住时间么 提交于 2020-01-17 07:17:10

问题


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:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/1981878/how-would-i-access-this-object-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!