__construct($ foo = NULL)无效

时间:2012-10-19 10:05:11

标签: php construct

我有奇怪的问题。这只是类的构造方法,不值得展示其他代码,反正看看这段代码:

Class xy {
    public $x = 10;
    public $y = 10; 

    public function __construct($x = NULL, $y = NULL) {
        if(isset($x) || isset($y)){
            $this->x = $x;      // assign center coords
            $this->y = $y;      // assign center coords
        }
        $this->area = $this->area();
        echo $this->x . " " . $this->y . " " . $this->area;
    }

}

从现在开始,我认为此代码应该回显$ this-> x和$ this-> y WITHOUT if(isset($x) || isset($y)){,如果没有使用此代码传入对象:$newObj = new xy;但事实并非如此。仅当此行看起来像$newObj = new xy(10,10);

时才有效

我需要帮助和澄清:)

2 个答案:

答案 0 :(得分:0)

我没有使用php多年,但我认为它应该看起来像$ newObj = new xy();

答案 1 :(得分:0)

change $newObj = xy(10,10); to $newObj = new xy(10,10);