Getter 和 Setter?[已关闭]
2022-08-30 06:16:08
我不是PHP开发人员,所以我想知道PHP使用显式getter/setters的优点和缺点是什么,以纯OOP风格,带有私有字段(我喜欢的方式):
class MyClass {
private $firstField;
private $secondField;
public function getFirstField() {
return $this->firstField;
}
public function setFirstField($x) {
$this->firstField = $x;
}
public function getSecondField() {
return $this->secondField;
}
public function setSecondField($x) {
$this->secondField = $x;
}
}
或只是公共字段:
class MyClass {
public $firstField;
public $secondField;
}