我将如何访问这个对象值? [英] How would I access this object value?

查看:40
本文介绍了我将如何访问这个对象值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试回显和访问存储在 ["_aVars:private"] 中的值

$obj->_vars 和 $obj->_vars:private 不起作用 :(

这是 $obj 的 var_dump

object(test_object)#15 (30) {[sDisplayLayout"]=>字符串(8)模板"[bIsSample"]=>布尔(假)[iThemeId"]=>整数(0)["sReservedVarname:protected"]=>字符串(6)测试"["sLeftDelim:protected"]=>字符串(1){"["sRightDelim:protected"]=>字符串(1)}"["_aPlugins:protected"]=>数组(0){}["_aSections:private"]=>数组(0){}["_aVars:private"]=>数组(56){[bUseFullSite"]=>布尔(假)[过滤器"]=>

解决方案

var_dump 输出的 :private 部分实际上不是成员名称的一部分,它表明 _aVars 成员是private.

因为 _aVars 是私有的,它的值不能从对象外部访问,只能从内部访问.

您需要一个公共 getter 函数或类似的东西来检索值.

编辑

为了帮助澄清这一点,我举了一个例子:

class testClass {公开 $x = 10;私人 $y = 0;}$obj = new testClass();echo "对象:";var_dump($obj);echo "公共财产:";var_dump($obj->x);echo "私有财产:";var_dump($obj->y);

以上代码产生以下输出:

对象:对象(测试类)[1]公共'x' =>整数 10私人'y' =>整数 0公共财产:整数 10私人财产:

注意在尝试了私有变量的 var_dump() 之后什么都没有.由于代码无法从外部访问 $obj->y,这意味着 var_dump() 无法访问它以生成有关它的任何信息.>

由于 $obj 是一个局部变量,var_dump() 在那里工作正常.var_dump() 的一个特殊特性是它将输出有关受保护和私有对象成员变量的信息,这就是您在对象转储中看到它的原因.但是,这并不意味着您可以访问它们.

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"]=>

解决方案

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.

这篇关于我将如何访问这个对象值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆