数组作为类变量,在类的构造中设置

时间:2013-06-12 10:56:22

标签: php arrays oop

有没有办法将数组传递给类/对象构造并访问/返回它或对其进行操作?

代码:

$affiliates = array(
        "clickbank" => "clickbankdata",
        "meepbank" => array(
                "meepbankdata" => "meep"
        )
);
class Affiliate {
    public $affiliates = array();

    public function __construct($array) {
        $this->affiliates = $array;
    }

    public function getAffiliatesArray() {
        return $this->$affiliates;
    }
}

$main = new Affiliate($affiliates);
print_r($main->getAffiliatesArray());

2 个答案:

答案 0 :(得分:3)

   public function getAffiliatesArray() {
            return $this->$affiliates;
        }

   should be

  public function getAffiliatesArray() {
        return $this->affiliates;
    }

答案 1 :(得分:2)

替换

return $this->$affiliates;
              ^
              |-------- Remove This 

return $this->affiliates;