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

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

4

1 回答 1

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视为数字索引,并被强制转换为数字(在 PHP 中)。由于,PHP 正在寻找错误。$attrib$index'blue' == 0$attrib[0] == 'b'$this->bUndefined property

于 2011-12-10T01:20:23.453 回答