数组作为静态类属性的奇怪行为

时间:2011-12-09 23:46:51

标签: php arrays class properties associative

我可以将关联数组作为类的属性吗?

我一直在尝试但是我得到的结果非常奇怪:

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()两次,而不是在类中设置属性,它会创建一个新属性。

我做错了什么?感谢任何帮助。

1 个答案:

答案 0 :(得分: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错误。