Objects and data structures


Objects and Data Structures

1. Use getters and setters Use object encapsulation

2. Use private or protected member variables for objects

1. Use getters and setters

In PHP, you can use public, protected, private for methods to control changes to object properties.

  1. When you want to perform operations other than obtaining the properties of an object, you do not need to find and modify each property access method in the code.

  2. When there is an attribute method corresponding to set, it is easy to add parameter verification

  3. Representation inside the package

  4. Easy to add logging and error control when using set and get

  5. When inheriting the current class, you can override the default method function

  6. When the object properties are obtained from the remote server, get*, set* are easy to use lazy loading

In addition, this approach also conforms to the opening and closing principle in OOP development.

bad:

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

good:

 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. Use private or protected member variables for objects

  • Modifying public methods and properties is very dangerous because external code can easily rely on it and you have no control over it. Modifications to this affect all users of this class. 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.

  • Modifications to protected are almost as dangerous as modifications to public, because they are available to subclasses. The only difference between them is that the callable locations are different, and modifications to them affect all places that integrate this class. 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 .

  • Modifications to private guarantee that this part of the code will only affect the current class. 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).

Therefore, use public/protected when you need to control whether the code in the class can be accessed, and use private at other times.

You can read this blog post by Fabien Potencier.

bad:

 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

good:

 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