2012-01-16 81 views
-2

编辑:谢谢大家。我甚至没有注意到它是私人的大声笑,所以我将它们从私人变为公共,现在应该可以访问......现在的问题是我如何获得说'背包定位'的价值?再次感谢!php访问对象值

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 
      ) 
+1

您是否从课堂范围之外访问它?私人实例变量的要点是它们不能被别的东西看到。 – 2012-01-16 18:49:43

+0

您无法访问班级的私人成员。这就是为什么他们是私人的。 – afuzzyllama 2012-01-16 18:52:00

+0

可能的重复:http://stackoverflow.com/questions/1981878/how-would-i-access-this-object-value – cspray 2012-01-16 18:53:25

回答

2

私人会员就是这样 - 私人。只有他们所属的类才可以访问它们。如果您希望能够检索其值,则需要使其受到保护(因此可用于父级和子级)或公共(可供所有班级使用)。另一种方法是写一些干将,看起来像

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

功能或使用__get()神奇的功能,使看起来像

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

更多信息可以在文档中的http://php.net/manual/en/language.oop5.visibility.php找到一个通用的getter

+0

虽然,制定一个getter魔术方法确实会破坏私有/受保护变量的整体目的。 – 2012-01-16 20:28:49

+0

并非总是如此。 __get()方法可以克隆它所返回的变量,从而使private/protected变量有效地为只读。 – 2012-01-16 21:07:01

0

您将需要访问的方法,每个对象以访问值。由于它们是私人的,因此只能在它们所属的每个类别中进行访问。

0

私人性质刚好可以从对象本身内部访问。要访问尝试使用$this->propertyName

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作品。