0
0
PhpComparisonBeginner · 4 min read

Public vs Private vs Protected in PHP: Key Differences and Usage

In PHP, 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 ModifierVisibilityAccessible FromInheritance AccessTypical Use Case
publicAnywhereAny codeYesMethods/properties meant for all
privateOnly inside classDefining class onlyNoInternal details, hidden data
protectedClass and subclassesClass and child classesYesShared 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
<?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();
?>
Output
red
↔️

Private and Protected Equivalent

Example showing private and protected usage in PHP:

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();
?>
Output
Engine: ENG123, Color: blue Color from child: blue
🎯

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

Use public for members accessible from any code.
Use private to hide members strictly inside the defining class.
Use protected to allow access in the class and its subclasses.
Access modifiers help protect data and control how classes interact.
Choosing the right visibility improves code safety and design clarity.