2011-12-09 5 views
1

클래스의 속성으로 연관 배열을 사용할 수 있습니까?배열에서 정적 클래스 속성으로 이상한 동작이 발생했습니다.

나는 시도하고있다 그러나 나는 이런 일에 아주 이상한 결과를 얻을 :

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

대신, 내가 갖는 :

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 
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() 때마다 전화를 볼 새 속성을 만듭니다.

내가 뭘 잘못하고 있니? A는 어떤 도움을 주셔서 감사합니다.

+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

관련 문제