我從中學習的書雖然並不總是必要的,但它通常被認爲是具有getter和setter方法的良好實踐。Get方法和Set方法在類中的區別
就是這樣說,每個例子中的方法在我啓動一個對象並傳遞一個值給方法時工作正常。
所以我只是想知道什麼是1
Class Something{
public $email;
function getEmail($newEmail){
return $this->email = $newEmail;
}
}
例以下
例之間的區別2
Class Something{
public $email
function setEmail($newEmail){
$this->email = $newEmail
}
function getEmail(){
return $this->email
}
}
例1
$obj = new Something();
echo $obj->getEmail('[email protected]'); //prints email address
例2
$obj->setEmail('[email protected]')
$obj->getEmail()//prints email adress
我看不出爲什麼額外的步驟是例如2必需的,也許是因爲我(非常)新OOP & PHP。我只是認爲在繼續之前瞭解它們之間的差異很重要,因爲我的書沒有詳細說明。