PHP类函数未定义索引

时间:2014-01-12 22:58:25

标签: php function class undefined

我有这个脚本。在主php文件中我创建了一个新的接管类,我addideastakeover()(到现在为止所有的一切)但是当我调用函数takeover()时它告诉我ideastakeover是一个未定义的变量。有人可以帮忙吗?

类别:

    class takeover {
    /**
     * @AttributeType int
     */
    private $idtakeover;
    /**
     * @AssociationType root
     * @AssociationMultiplicity 1
     */
    public $_unnamed_root_;

    public $ideiastakeover=array();

    public function __construct($idtakeover){
        $this->idtakeover = $idtakeover;
    }
    /**
     * @access public
     */
    public function getidcompraroot() {
        // Not yet implemented
    }

    public function addideiastakeover($idtakeover, $idideia) {
        $this->idtakeover = $idtakeover;
        $this->idideia = $idideia;
        array_push($this->ideiastakeover,$idideia);
        //print join(" ",$this->ideiastakeover);
    }

        /**
     * @access public
     */
    public function takeover() {
        echo $ideiastakeover[1];
    }
}

我如何在主脚本上调用它:

$takeover = new takeover(1);

    for ($i=0; $i<$conta; $i++ ){
        $takeover->addideiastakeover(1,$idsideias[$i]);
    }

    $takeover->takeover(); //PROBLEM ON THIS LINE

有人可以帮忙吗?谢谢:))

2 个答案:

答案 0 :(得分:0)

你必须在课堂内使用$ this:

echo $this->ideiastakeover[1];

答案 1 :(得分:0)

takeover()更改为:

public function takeover() {
    echo $this->ideiastakeover[1];
}