php访问对象值

时间:2012-01-16 18:46:26

标签: php object

编辑:谢谢大家。我甚至没有注意到它是私人大声笑,所以我把它们从私人改为公共,现在它应该是可访问的...现在的问题是如何才能访问说'backpackPosition'的价值?再次感谢!

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
            )

6 个答案:

答案 0 :(得分:2)

私人会员只是私密会员。只有他们所属的类才能访问它们。如果您希望能够检索它们的值,则需要使它们受到保护(因此可用于父类和子类)或公共(适用于所有类)。另一种选择是编写一些看起来像

的getter
public function get_slot() {
    return $this->slot;
}

或使用__get()魔术函数制作看似

的通用吸气剂
public function __get($name) {
    return $this->$name;
}

更多信息可在http://php.net/manual/en/language.oop5.visibility.php

的文档中找到

答案 1 :(得分:0)

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

http://www.php.net/manual/en/language.oop5.properties.php

答案 2 :(得分:0)

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

答案 3 :(得分:0)

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

答案 4 :(得分:0)

这个答案适用于尝试绕过强加的私有数据限制的情况,例如,如果您偶然使用您无权访问的库来更改类成员的权限级别,那么是一个解决方法。假设对象是可序列化/不可序列化的,那么请考虑:

<?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));    

输出类似于:

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";}"

因此,一个解决方案是创建一个重复的类,将变量的权限级别更改为所有公共。然后序列化工作对象,将序列化类转换为你的版本,然后简单地反序列化字符串,你将拥有一个具有无限访问权限的类类型的工作对象。

显然,转换方法是你必须做一些脚步工作的地方。您需要构建一个可以处理任何案例的通用解析器,或者您可以为您的特定用例系列str_replaces编写hacky函数。

答案 5 :(得分:-1)