2012-01-16 171 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作品。