Public mutable field (ARCH7186)
هذا المحتوى غير متوفر بلغتك بعد.
Key php.quality.public-mutable-field · Severity warning · Category quality
Public mutable fields break encapsulation and allow uncontrolled external mutation. Hide the field and expose it through guarded methods.
Incorrect
Section titled “Incorrect”<?phpclass Account{ public float $balance = 0.0;}Correct
Section titled “Correct”<?phpclass Account{ private float $balance = 0.0;
public function deposit(float $amount): void { if ($amount <= 0) throw new InvalidArgumentException('positive only'); $this->balance += $amount; }
public function balance(): float { return $this->balance; }}Make the field private and expose access through validating getter/mutator methods.