我有一個奇怪的問題。這就是類的只是構造方法,不值得以顯示其它代碼,反正,看一下這個代碼:
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)
;
我需要幫助澄清:)
將'isset()'改爲'!is_null()'help? – Passerby
'$ x === NULL || $ this-> x = $ x;'對於第二個參數是相同的。只有在沒有默你需要爲每個參數做這件事。 – hakre
@Passerby,'isset'在邏輯上等同於'!is_null'(它們的返回值對於所有輸入都是相同的)。 – goat