CodeBlanks Logo Icon CodeBlanks

private access

PHP course · Intermediate · Lesson 26/115

Learn that private access is limited to the defining class only.

The private access modifier

private is the strictest visibility. When a property or method is private, it can only be accessed inside the class that declares it. Even subclasses cannot reach it.

For example, class Counter { private int $count = 0; } means $count is only reachable from Counter's own methods via $this->count. Trying to access it from outside or from a subclass triggers a fatal error.

private is used when you want to fully hide a class's internal implementation. It is the cornerstone of encapsulation and prevents outside code from breaking internal state.

private cannot be reached from subclasses

Consider this example: start with class Account { private float $balance = 0; }. If you then declare class Savings extends Account { public function peek() { return $this->balance; } }, calling Savings->peek() triggers a fatal error.

Because $balance is private, Savings cannot access it even though it extends Account. This is the core difference between protected and private: protected is open to subclasses, private is only to the declaring class.

If a subclass needs this data, the parent must expose a public or protected method, or change the property to protected.

Exercise Counter class with a private property

Define a Counter class with a private int $count = 0 property. Add a public function increment(): void method that does $this->count++. Also add public function getCount(): int that does return $this->count. Then create $counter = new Counter(), call $counter->increment() three times and print echo $counter->getCount() . "\n".

Show example solution
Declare private $count, increment does $this->count++, getCount returns $this->count.
Example solution:
<?php
// Define a Counter class with a private property
class Counter {
    private int $count = 0;
    public function increment(): void {
        $this->count++;
    }
    public function getCount(): int {
        return $this->count;
    }
}
$counter = new Counter();
$counter->increment();
$counter->increment();
$counter->increment();
echo $counter->getCount() . "\n";

Test yourself

  1. Declare a private property

    class Counter {
        ____
    }
    Show answer
    class Counter {
        private $count;
    }
  2. What happens if a subclass tries to reach a private property?

    • It can, because subclasses inherit
    • Only if it is also marked protected
    • Fatal error: Cannot access private property
    • It warns but still reaches it
    Show answer

    Fatal error: Cannot access private property — Correct. private members cannot be reached even from subclasses; PHP throws a fatal error.

  3. Match the visibility modes

    private $count
    Declaring class only
    public $count
    Accessible everywhere
    protected $count
    Class and subclasses
    var $count
    Legacy, public

All lessons in Intermediate