What is the difference between public, private, and protected?

What is the difference between public, private, and protected?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

What is the difference between public, private, and protected?

In object-oriented programming, access modifiers are the primary mechanism for enforcing encapsulation. They determine how class members interact with the outside world and, crucially, how subclasses inherit and utilize those members. The three standard modifiers—public, private, and protected—are not just syntactic sugar; they represent distinct contracts regarding visibility and inheritance.

Public: The Contract

When a property or method is marked as public, it is accessible from anywhere. Any code that has an instance of the class can read or modify the variable, or invoke the function.

class User {
    public $name;

    public function greet() {
        return "Hello, I am {$this->name}";
    }
}

In this example, the $name property and greet() method are part of the class's interface. If you instantiate a User object, you can directly access these members without needing any special permissions. This is the default behavior in many languages, but explicitly declaring them as public signals intent. It tells other developers that this member is expected to be used by external code.

However, making everything public often leads to tight coupling and fragile architecture. If your internal logic changes—say, you refactor how $name is stored or validated—any code touching that property breaks. Public members assume the responsibility of maintaining their own invariants if they aren't backed by a constructor or setter logic, which can lead to inconsistent object states.

Private: The Hidden Implementation

Conversely, private restricts access strictly to the class where the member is defined. Code outside that class cannot read or modify the property, nor can it call the method directly.

class BankAccount {
    private $balance;

    public function deposit(int $amount) {
        $this->validateAmount($amount); // Helper method also private
        $this->balance += $amount;
    }

    private function validateAmount(int $amount): void {
        if ($amount <= 0) {
            throw new InvalidArgumentException('Invalid amount');
        }
    }
}

Here, the $balance is an implementation detail. External code cannot accidentally set a negative balance or bypass validation logic because they cannot touch $this->balance. The validateAmount method is also private; it supports the public API but isn't part of it.

The trade-off is that private members can be tricky to test in isolation and harder to debug via inspection if you don't know exactly which methods are accessing them internally. In languages like PHP, you cannot rely on magic methods (like __get) to proxy access to private properties unless you explicitly define them. Therefore, the design must ensure all necessary logic remains public if external interaction is required.

Protected: The Inheritance Bridge

The distinction between private and protected lies in inheritance. A protected member is visible to the class itself and any subclasses that extend it.

class Animal {
    protected $legs = 4;

    public function move() {
        echo "Moving with {$this->legs} legs";
    }
}

class Dog extends Animal {
    // Can access $this->legs and override $move()
    // Cannot access new methods added to Animal if they are private
}

In this scenario, Dog can read or modify $legs. It might even override the move() method to add barking logic. If $legs were private, Dog would be forced to accept a default value or use reflection (which is generally discouraged) to access it.

The primary use case for protected is when you want a base class to provide shared state or behavior that derived classes need to inspect or alter, but you don't want the general public to touch it directly. This creates a "middle ground" where the member is part of the inheritance hierarchy but remains shielded from external misuse.

Choosing the Right Modifier

The decision usually comes down to one question: Who needs to see this?

  • Use public only for the interface—methods users call and data they expect to manipulate directly.
  • Use private for internal state, helper methods, and logic that should never be bypassed or inspected by outside code.
  • Use protected when you are designing a hierarchy where child classes must know about certain properties or override specific behaviors, but those details should remain hidden from the rest of the application.

Frameworks like Laravel often rely heavily on these distinctions to provide robust services that can be extended safely without exposing internal implementation details. Understanding these boundaries ensures your classes remain flexible and secure as your architecture evolves.

Our network