2012-10-19 72 views
1

我有一個奇怪的問題。這就是類的只是構造方法,不值得以顯示其它代碼,反正,看一下這個代碼:

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; 
    } 

} 

因爲現在,我認爲此代碼應回聲$這 - > x和$這個 - >ÿ沒有if(isset($x) || isset($y)){,如果它沒有通過該代碼傳遞給對象:但它沒有。它只適用於這條線看起來像這樣$newObj = new xy(10,10);

我需要幫助澄清:)

+1

將'isset()'改爲'!is_null()'help? – Passerby

+0

'$ x === NULL || $ this-> x = $ x;'對於第二個參數是相同的。只有在沒有默你需要爲每個參數做這件事。 – hakre

+0

@Passerby,'isset'在邏輯上等同於'!is_null'(它們的返回值對於所有輸入都是相同的)。 – goat

回答

0

我haven`t使用PHP多年,但我認爲它應該看起來像$ newObj =新XY();

+1

剛試過你的代碼$ newObj = new xy();它工作正常。 –

+0

問題是,我認爲,它應該在代碼中沒有第6和第9行,不是嗎? – Rozkalns

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