2011-12-09 66 views
1

可以將關聯數組作爲類的屬性嗎?將數組作爲靜態類屬性的奇怪行爲

我一直在努力,但我得到的東西非常奇怪的結果是這樣的:

class MyClass { 
    public static $quality = array('blue' => 20, 'green' => 30, 'yellow' => 40); 
    public $car = array(); 
    public $bus = array(); 

    function __construct() { 
    foreach(self::$quality as $key => $value) { 
     $this->car[$key] = $value; 
     $this->bus[$key] = $value; 
     echo $this->car[$key] . '<br />'; 
     echo $this->bus[$key] . '<br />'; 
    } 
    foreach(self::$quality as $key => $value) { 
     echo $this->car[$key] . ' - ' . $this->bus[$key] . '<br />'; 
    } 
    } 
} 

我期待這樣的輸出:

20 
20 
30 
30 
40 
40 
40 - 40 
30 - 30 
20 - 20 

而是我越來越:

20 
20 
30 
3 
40 
4 
20 - 4 
30 - 4 
40 - 4 

就是這樣。只有綠色和黃色的公交車錯過了第二位......

,並在我的類中的不同點,當我有這樣的事情:

$attrib = 'bus'; 
$index = 'blue'; 
echo $this->$attrib[$index]; 

我得到

未定義的屬性MyClass :: $ b

任何提示我做錯了什麼? 或PHP不接受關聯數組作爲類的屬性?任何幫助將不勝感激。


這是我孤立的代碼。如果你本身運行這個PHP你應該得到的奇怪的行爲...

<?php 
    ini_set('display_errors',1); 
    error_reporting(E_ALL); 

    class Session 
    { 
     public static $per_page_count = array('photo' => 8 ,'book' => 20, 'road' => 30, 'lodge' => 30, 'tag' => 50); 
     public $current_page = array(); 
     public $per_page = array(); 
     public $total_count = array(); 

     function __construct() 
     { 
      foreach (self::$per_page_count as $page_key => $page_count) 
      { 
       if (isset($this->per_page[$page_key])) 
       { 
        if ($this->per_page[$page_key] == '') $this->per_page[$page_key] = $page_count; 
       } 
       else $this->per_page[$page_key] = $page_count; 
       if (isset($this->current_page[$page_key])) 
       { 
        if ($this->current_page[$page_key] == '') $this->current_page[$page_key] = 1; 
       } 
       else $this->current_page[$page_key] = 1; 
       $this->total_count[$page_key] = 1; 
      } 
     } 

     public function get_set($attribute,$index='',$value='') 
     { 
      echo '<br />before - '.$attribute.'-'.$index.'-'.$value.'<br />'; 
      if (trim($value) != '') 
      { 
       if (property_exists($this,$attribute)) 
       { 
        $this->$attribute['aaa'] = 50; 
        if ($index != '') 
        { 
         $_SESSION[$attribute][$index] = $value; 
         $this->$attribute[$index] = $value; 
        } 
        else 
         $_SESSION[$attribute] = $this->$attribute = $value; 

        $arraykey = array_keys($this->$attribute); 
       } 
      } 
      else 
      { 
       if ($index != '') 
        return $this->$attribute[$index]; 
       else 
        return $this->$attribute; 
      } 
      echo '<pre>'; 
      print_r($this); 
      echo '</pre>'; 
      echo '<br />after - '.$attribute.'-'.$index.'-'.$value.'<br />'; 
      echo '<br />variable - '.$this->$attribute[$index].'-'.$value.'<br />'; 
     } 
    } 

    $session = new Session(); 
    $session->get_set('current_page','photo',4); 
    $session->get_set('total_count','photo',150); 
?> 

在底部,你看,我稱之爲get_set()兩次,每一次,而不是設置在類中的屬性,它創建一個新的屬性。

我在做什麼錯?感謝任何幫助。

+0

你的代碼似乎對我來說很好,除了一些語法錯誤:http://codepad.viper-7.com/2rt3hT –

+0

作爲屬性的關聯數組很好。你能發佈實際的代碼嗎?很難調試你剛纔輸入並編輯過的代碼。 – webbiedave

回答

0

錯誤:

$attrib = 'bus'; 
$index = 'blue'; 
echo $this->$attrib[$index]; 

正確:

$attrib = 'bus'; 
$index = 'blue'; 
$hash = $this->$attrib 
echo $hash[$index]; 

發生了什麼錯誤的版本?首先,可以按位置查找字符串中的字符。例如:

$str = 'abcdefg'; 
echo $str[1]; // outputs 'b' 

$attrib[$index]正在被首先評估。 PHP將$index作爲$attrib字符串的數字索引,其中$index被轉換爲數字(在PHP 'blue' == 0中)。由於$attrib[0] == 'b',PHP正在尋找$this->b因此出現Undefined property錯誤。

+0

非常感謝。您的解決方案奏效我猜測這是問題所在,但我認爲我的大腦太累了,看不到解決方案。再一次非常感謝你。 –

+0

太棒了!我可以得到upvote嗎? – leepowers