xhqiang 2019-06-25
在 PHP 中,通过为属性或方法设置 public, protected 和 private 关键字可以实现对属性或方法的可见性控制。不过,通过 getter 和 setter 也可以达到控制可见性的目的,并且在某些场景下它具备一些额外的好处。
使用 getter和 setter 有以下好处:
另外,这是面向对象的基本设计原则中的开放/封闭原则。
Bad:
class BankAccount { public $balance = 1000; } $bankAccount = new BankAccount(); // 买了一双鞋... $bankAccount->balance -= 100;
Good:
class BankAccount { private $balance; public function __construct($balance = 1000) { $this->balance = $balance; } //做一些事情 public function withdrawBalance($amount) { if ($amount > $this->balance) { throw new \Exception('Amount greater than available balance.'); } $this->balance -= $amount; } public function depositBalance($amount) { $this->balance += $amount; } public function getBalance() { return $this->balance; } } $bankAccount = new BankAccount(); // 买了一双鞋... $bankAccount->withdrawBalance($shoesPrice); // 获取结余 $balance = $bankAccount->getBalance();
Bad:
class Employee { public $name; public function __construct($name) { $this->name = $name; } } $employee = new Employee('John Doe'); echo 'Employee name: '.$employee->name; // Employee name: John Doe
Good:
class Employee { private $name; public function __construct($name) { $this->name = $name; } public function getName() { return $this->name; } } $employee = new Employee('John Doe'); echo 'Employee name: '.$employee->getName(); // Employee name: John Doe
这里不是说不使用继承,使用“组合模式”和使用“继承”都有很多好的理由。
这里想说的是当你本能的要使用继承时就想一想“组合模式”是否能更好帮你的解决问题。
那么,你可能想知道,“什么时候应该用继承?”, 这取决于你手头上问题。
以下几点说明了什么时候使用继承会更合适。
Bad:
class Employee { private $name; private $email; public function __construct($name, $email) { $this->name = $name; $this->email = $email; } // ... } // 因为雇员和税收不是对等关系而是包含的关系 // 所以这里应用组合比较合适 class EmployeeTaxData extends Employee { private $ssn; private $salary; public function __construct($name, $email, $ssn, $salary) { parent::__construct($name, $email); $this->ssn = $ssn; $this->salary = $salary; } // ... }
Good:
class EmployeeTaxData { private $ssn; private $salary; public function __construct($ssn, $salary) { $this->ssn = $ssn; $this->salary = $salary; } // ... } class Employee { private $name; private $email; private $taxData; public function __construct($name, $email) { $this->name = $name; $this->email = $email; } public function setTaxData($ssn, $salary) { $this->taxData = new EmployeeTaxData($ssn, $salary); } // ... }
class Singleton: def __new__: # 关键在于这,每一次实例化的时候,我们都只会返回这同一个instance对象 if not hasattr: cls.instance =