php访问对象值

php访问对象值,php,object,Php,Object,编辑:谢谢大家。我甚至没有注意到它是私有lol,所以我把它们从私有改为公共,现在应该可以访问了。。。现在的问题是我如何访问say'backpacketposition'的值?再次感谢 TF2Inventory Object ( [fetchDate] => 123456123 [items] => Array ( [60] => TF2Item Object ( [equipped] => A

编辑:谢谢大家。我甚至没有注意到它是私有lol,所以我把它们从私有改为公共,现在应该可以访问了。。。现在的问题是我如何访问say'backpacketposition'的值?再次感谢

TF2Inventory Object
(
[fetchDate] => 123456123
[items] => Array
    (
        [60] => TF2Item Object
            (
                [equipped] => Array
                    (
                        [scout] => 1
                        [sniper] => 1
                        [soldier] => 1
                        [demoman] => 1
                        [medic] => 1
                        [heavy] => 1
                        [pyro] => 1
                        [spy] => 1
                    )

                [attributes] => Array
                    (
                        [0] => stdClass Object
                            (
                                [name] => custom employee number
                                [class] => set_employee_number
                                [value] => 0
                            )

                        [1] => stdClass Object
                            (
                                [name] => cannot trade
                                [class] => cannot_trade
                                [value] => 1
                            )

                    )

                [backpackPosition] => 61
                [className] => tf_wearable
                [count] => 1
                [defindex] => 170
                [id] => 535518002
                [level] => 20
                [name] => Primeval Warrior
                [quality] => unique
                [slot] => misc
                [tradeable] => 
                [type] => Badge
            )

        [43] => TF2Item Object
            (
                [equipped] => Array
                    (
                        [scout] => 0
                        [sniper] => 0
                        [soldier] => 0
                        [demoman] => 0
                        [medic] => 0
                        [heavy] => 0
                        [pyro] => 0
                        [spy] => 0
                    )

                [attributes] => Array
                    (
                        [0] => stdClass Object
                            (
                                [name] => cannot trade
                                [class] => cannot_trade
                                [value] => 1
                            )

                    )

                [backpackPosition] => 44
                [className] => tf_wearable
                [count] => 1
                [defindex] => 471
                [id] => 535518003
                [level] => 50
                [name] => Proof of Purchase
                [quality] => unique
                [slot] => head
                [tradeable] => 
                [type] => Hat
            )

        [42] => TF2Item Object
            (
                [equipped] => Array
                    (
                        [scout] => 1
                        [sniper] => 1
                        [soldier] => 1
                        [demoman] => 1
                        [medic] => 1
                        [heavy] => 1
                        [pyro] => 1
                        [spy] => 1
                    )

                [attributes] => 
                [backpackPosition] => 43
                [className] => tf_wearable
                [count] => 1
                [defindex] => 278
                [id] => 541628464
                [level] => 31
                [name] => Horseless Headless Horsemann's Head
                [quality] => unique
                [slot] => head
                [tradeable] => 
                [type] => Hat
            )

        [59] => TF2Item Object
            (
                [equipped] => Array
                    (
                        [scout] => 0
                        [sniper] => 0
                        [soldier] => 0
                        [demoman] => 0
                        [medic] => 0
                        [heavy] => 0
                        [pyro] => 0
                        [spy] => 0
                    )

                [attributes] => Array
                    (
                        [0] => stdClass Object
                            (
                                [name] => cannot trade
                                [class] => cannot_trade
                                [value] => 1
                            )

                    )

                [backpackPosition] => 60
                [className] => tf_wearable
                [count] => 1
                [defindex] => 115
                [id] => 548155039
                [level] => 10
                [name] => Mildly Disturbing Halloween Mask
                [quality] => unique
                [slot] => head
                [tradeable] => 
                [type] => Holiday Hat
            )

这些项目只能由对象本身访问。您必须修改该类的代码并提供访问器方法,或者更改其作用域


您需要对每个对象使用accessor方法才能访问这些值。因为它们是私有的,所以只能在它们所属的每个类中访问它们。

私有成员就是这样-私有的。只有它们所属的类才能访问它们。如果希望能够检索它们的值,则需要将它们设置为受保护的(从而可供父类和子类使用)或公共的(可供所有类使用)。另一个选择是编写一些getter,函数

public function get_slot() {
    return $this->slot;
}
或者使用
\uu get()
魔术函数生成一个如下所示的通用getter

public function __get($name) {
    return $this->$name;
}

更多信息可在

的文档中找到。您应该看到第一个面向对象的php

私有属性可以从对象本身内部访问。要访问,请尝试使用
$this->propertyName

此答案适用于试图绕过强加的私有数据限制的场景,例如,如果您碰巧正在使用一个您无权更改类成员特权级别的库,则需要解决此问题。假定对象可序列化/不可序列化,然后考虑:

<?php
class SourceProtected {
    private $foo = 'one';
    protected $baz = 'two';
    public $bar = 'three';
}   

class SourceUnprotected {
    public $foo = 'blah';       
    public $baz = 'two';
    public $bar = 'three';
}


$protected = new SourceProtected();
$unprotected = new SourceUnprotected();
var_dump(serialize($protected), serialize($unprotected));    
因此,一种解决方案是创建一个复制类,将变量的特权级别更改为“所有公共”。然后序列化工作对象,将序列化的类转换为您的版本,然后简单地取消序列化字符串,您将拥有一个具有无限访问权限的类类型的工作对象


显然,转换方法是您需要做一些脚部工作的地方。您需要构建一个可以处理任何情况的通用解析器,或者您可以为str_替换的特定用例系列编写一个hacky works

您是否从类的范围之外访问它?私有实例变量的要点是它们不能被其他任何东西看到。您不能访问类的私有成员。这就是为什么它们是私有的。可能的重复:但是,制作一个getter-magic方法确实会破坏私有/受保护变量的全部用途。并非总是如此。方法可以克隆它返回的变量,使私有/受保护的变量有效地成为只读变量。
string(110) "O:15:"SourceProtected":3:{s:20:"?SourceProtected?foo";s:3:"one";s:6:"?*?baz";s:3:"two";s:3:"bar";s:5:"three";}"
string(92) "O:17:"SourceUnprotected":3:{s:3:"foo";s:4:"blah";s:3:"baz";s:3:"two";s:3:"bar";s:5:"three";}"