Public vs Private vs Protected in PHP: Key Differences and Usage
public members are accessible from anywhere, private members are accessible only within the defining class, and protected members are accessible within the class and its subclasses. These keywords control visibility and help encapsulate data and behavior in object-oriented programming.Quick Comparison
Here is a quick overview of how public, private, and protected differ in PHP:
| Access Modifier | Visibility | Accessible From | Inheritance Access | Typical Use Case |
|---|---|---|---|---|
| public | Anywhere | Any code | Yes | Methods/properties meant for all |
| private | Only inside class | Defining class only | No | Internal details, hidden data |
| protected | Class and subclasses | Class and child classes | Yes | Shared internal use in hierarchy |
Key Differences
Public members are the most open and can be accessed from any part of the program, including outside the class. This makes them suitable for methods or properties that form the class's interface.
Private members restrict access strictly to the class where they are declared. Neither child classes nor external code can access them. This is useful for hiding implementation details and protecting data integrity.
Protected members strike a balance by allowing access within the class and any subclasses. This supports inheritance by letting child classes use or modify these members while still hiding them from outside code.
Code Comparison
Example showing public usage in PHP:
<?php class Car { public $color; public function setColor($color) { $this->color = $color; } public function getColor() { return $this->color; } } $car = new Car(); $car->setColor('red'); echo $car->getColor(); ?>
Private and Protected Equivalent
Example showing private and protected usage in PHP:
<?php class Car { private $engineNumber; protected $color; public function __construct($engineNumber, $color) { $this->engineNumber = $engineNumber; $this->color = $color; } private function getEngineNumber() { return $this->engineNumber; } protected function getColor() { return $this->color; } public function showDetails() { // Accessing private and protected inside the class return 'Engine: ' . $this->getEngineNumber() . ', Color: ' . $this->getColor(); } } class SportsCar extends Car { public function showColor() { // Can access protected property return 'Color from child: ' . $this->color; } } $car = new SportsCar('ENG123', 'blue'); echo $car->showDetails() . "\n"; echo $car->showColor(); ?>
When to Use Which
Choose public when you want properties or methods to be accessible from anywhere, such as the main interface of your class. Use private to hide sensitive data or internal logic that should not be changed or accessed outside the class itself. Opt for protected when you want to allow child classes to access or modify members but keep them hidden from outside code, supporting safe inheritance.
Key Takeaways
public for members accessible from any code.private to hide members strictly inside the defining class.protected to allow access in the class and its subclasses.