PHP OOP Access Modifier


In PHP Object-Oriented Programming (OOP), access modifiers are keywords used to control the visibility and accessibility of properties and methods within a class. They determine where and how these class members can be accessed. PHP provides three main access modifiers: public, protected, and private.

Types of Access Modifiers

  1. Public (public)
  2. Protected (protected)
  3. Private (private)

Each access modifier has a different level of restriction for accessing class members:

1. Public

  • Members declared as public can be accessed from anywhere: within the class itself, in derived (child) classes, and from outside the class.
  • It is the most permissive access modifier.

Example:

class Car { public $color; public function setColor($color) { $this->color = $color; } public function getColor() { return $this->color; } } $myCar = new Car(); $myCar->setColor("red"); echo $myCar->getColor(); // Output: red

In this example, both the property $color and the methods setColor() and getColor() are declared as public, allowing them to be accessed from outside the Car class.

2. Protected

  • Members declared as protected can be accessed within the class and by inheriting (child) classes, but not from outside the class.
  • This level of access provides a way to share properties and methods between related classes (parent and child) while still hiding them from the rest of the application.

Example:

class Animal { protected $species; public function setSpecies($species) { $this->species = $species; } } class Dog extends Animal { public function getSpecies() { return $this->species; } } $myDog = new Dog(); $myDog->setSpecies("Canine"); echo $myDog->getSpecies(); // Output: Canine // echo $myDog->species; // Error: Cannot access protected property directly from outside the class

In this example:

  • The property $species is declared as protected, so it cannot be accessed directly from outside the Animal or Dog classes. However, it can be accessed within the Dog class through the getSpecies() method.

3. Private

  • Members declared as private can be accessed only within the class where they are defined.
  • They are not accessible in derived (child) classes or from outside the class.
  • This level of access is used to encapsulate data, ensuring that it cannot be changed directly by other parts of the program.

Example:

class BankAccount { private $balance = 0; public function deposit($amount) { if ($amount > 0) { $this->balance += $amount; echo "Deposited: $amount<br>"; } } public function getBalance() { return $this->balance; } } $account = new BankAccount(); $account->deposit(100); // Output: Deposited: 100 echo $account->getBalance(); // Output: 100 // echo $account->balance; // Error: Cannot access private property directly

In this example:

  • The property $balance is declared as private, so it can only be accessed or modified from within the BankAccount class.
  • The deposit() and getBalance() methods allow controlled access to the balance.


Why Use Access Modifiers?

Access modifiers play an important role in implementing encapsulation, which is one of the fundamental principles of OOP. They help in:

  1. Protecting Data: You can hide data to prevent it from being modified directly, avoiding unintended side effects.
  2. Controlling Access: Access modifiers provide a way to control who can access certain properties and methods.
  3. Encapsulation: By restricting access, you ensure that an object's internal state is only modified in intended ways, often through public methods that add validation.

Practical Example Using All Access Modifiers

class Employee { public $name; // Accessible from anywhere protected $position; // Accessible within the class and its children private $salary; // Only accessible within the class public function __construct($name, $position, $salary) { $this->name = $name; $this->position = $position; $this->salary = $salary; } // Public method to get the salary (read-only access) public function getSalary() { return $this->salary; } // Public method to set the salary (controlled access) public function setSalary($salary) { if ($salary > 0) { $this->salary = $salary; } } } class Manager extends Employee { public function promote() { $this->position = "Senior " . $this->position; // Allowed because $position is protected } } $employee = new Employee("John Doe", "Developer", 50000); $manager = new Manager("Jane Doe", "Manager", 60000); echo $employee->name; // Output: John Doe $manager->promote(); // Promotion allowed within derived class echo $manager->getSalary(); // Output: 60000 // echo $manager->salary; // Error: Cannot access private property directly

In this example:

  • $name is a public property and can be accessed from anywhere.
  • $position is protected, so it can be accessed within the Employee class and the Manager class.
  • $salary is private, ensuring that it cannot be accessed directly but can be retrieved or updated using methods with controlled logic.

Summary

  • Public members can be accessed from anywhere.
  • Protected members can be accessed only within the class and its derived classes.
  • Private members can only be accessed within the class where they are defined.
  • Access modifiers help enforce encapsulation, ensuring controlled access to class members, which makes your code more secure, maintainable, and less prone to errors.

Access modifiers are essential for controlling how different parts of your application interact with your classes and their members, promoting best practices in OOP by enforcing a clear and predictable interface for each class.