亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

How to clean your PHP code / 對象和數(shù)據(jù)結(jié)構(gòu)

對象和數(shù)據(jù)結(jié)構(gòu)

對象和數(shù)據(jù)結(jié)構(gòu) Objects and Data Structures

1. 使用 getters 和 setters Use object encapsulation

2. 給對象使用私有或受保護(hù)的成員變量

1. 使用 getters 和 setters

在PHP中你可以對方法使用public, protected, private 來控制對象屬性的變更。

  1. 當(dāng)你想對對象屬性做獲取之外的操作時,你不需要在代碼中去尋找并修改每一個該屬性訪問方法

  2. 當(dāng)有set對應(yīng)的屬性方法時,易于增加參數(shù)的驗證

  3. 封裝內(nèi)部的表示

  4. 使用setget時,易于增加日志和錯誤控制

  5. 繼承當(dāng)前類時,可以復(fù)寫默認(rèn)的方法功能

  6. 當(dāng)對象屬性是從遠(yuǎn)端服務(wù)器獲取時,get*,set*易于使用延遲加載

此外,這樣的方式也符合OOP開發(fā)中的開閉原則

壞:

 class BankAccount
{
    public $balance = 1000;
}
 
$bankAccount = new BankAccount();
 
// Buy shoes...
$bankAccount->balance -= 100;

好:

 class BankAccount
{
    private $balance;
 
    public function __construct(int $balance = 1000)
    {
      $this->balance = $balance;
    }
 
    public function withdraw(int $amount): void
    {
        if ($amount > $this->balance) {
            throw new \Exception('Amount greater than available balance.');
        }
 
        $this->balance -= $amount;
    }
 
    public function deposit(int $amount): void
    {
        $this->balance += $amount;
    }
 
    public function getBalance(): int
    {
        return $this->balance;
    }
}
 
$bankAccount = new BankAccount();
 
// Buy shoes...
$bankAccount->withdraw($shoesPrice);
 
// Get balance
$balance = $bankAccount->getBalance();

2. 給對象使用私有或受保護(hù)的成員變量

  • 對public方法和屬性進(jìn)行修改非常危險,因為外部代碼容易依賴他,而你沒辦法控制。對之修改影響所有這個類的使用者。 public methods and properties are most dangerous for changes, because some outside code may easily rely on them and you can't control what code relies on them. Modifications in class are dangerous for all users of class.

  • 對protected的修改跟對public修改差不多危險,因為他們對子類可用,他倆的唯一區(qū)別就是可調(diào)用的位置不一樣,對之修改影響所有集成這個類的地方。 protected modifier are as dangerous as public, because they are available in scope of any child class. This effectively means that difference between public and protected is only in access mechanism, but encapsulation guarantee remains the same. Modifications in class are dangerous for all descendant classes.

  • 對private的修改保證了這部分代碼只會影響當(dāng)前類private modifier guarantees that code is dangerous to modify only in boundaries of single class (you are safe for modifications and you won't have Jenga effect).

所以,當(dāng)你需要控制類里的代碼可以被訪問時才用public/protected,其他時候都用private

可以讀一讀這篇 博客文章 ,F(xiàn)abien Potencier寫的.

壞:

 class Employee
{
    public $name;
 
    public function __construct(string $name)
    {
        $this->name = $name;
    }
}
 
$employee = new Employee('John Doe');
echo 'Employee name: '.$employee->name; // Employee name: John Doe

好:

 class Employee
{
    private $name;
 
    public function __construct(string $name)
    {
        $this->name = $name;
    }
 
    public function getName(): string
    {
        return $this->name;
    }
}
 
$employee = new Employee('John Doe');
echo 'Employee name: '.$employee->getName(); // Employee name: John Doe