如何使用列表和数组来实例化类中的字段?

时间:2012-07-10 08:41:06

标签: php arrays list

我想同时实例化我的PHP类的每个字段。到目前为止,我已尝试使用list。这是我下面的代码(不起作用)。任何帮助都会很乐意接受。

class Test {
  private $x;
  private $y;
  private $z;

  public function Test() {
    $fields = array($this->x, $this->y, $this->z);
    // I need $fields to be an array here.
    list($fields) = array(1, 2, 3); // It would work if $fields wasn't an array.
    echo 'x = ' . $this->x . '<br />y = ' . $this->y . '<br />z = ' . $this->z;
  }
}

new Test();

1 个答案:

答案 0 :(得分:2)

list($this->x, $this->y, $this->z) = array(1, 2, 3);