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() を 2 回呼び出し、そのたびに、クラス内でプロパティを設定する代わりに、新しいプロパティを作成していることがわかります。

私は何を間違っていますか?助けていただければ幸いです。

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 は、数値にキャストされて (PHP で)文字列$indexの数値インデックスとして扱われます。以来、PHPはエラーを探しています。$attrib$index'blue' == 0$attrib[0] == 'b'$this->bUndefined property

于 2011-12-10T01:20:23.453 に答える