W3docs

private

The "private" keyword is used in PHP to declare a class member as private, meaning that it can only be accessed within the class itself. In this article, we

The PHP private Keyword

private is a visibility modifier (also called an access modifier). When you mark a property or method private, it can be read and called only from inside the same class — never from outside the object, and never from a child class that extends it.

Visibility is the core of encapsulation: it lets a class hide its internal data and helper logic behind a controlled public surface, so the outside world depends only on what you choose to expose. PHP has three visibility levels:

ModifierSame classChild classOutside the class
publicyesyesyes
protectedyesyesno
privateyesnono

This page covers the syntax of private, why and when to use it, and the gotchas that trip people up — inheritance, the "same class, different object" rule, and error behavior. For the bigger picture, see PHP Access Modifiers and PHP Classes and Objects.

Syntax

Prefix the property or method declaration with the private keyword:

class MyClass {
  private $myPrivateVariable;

  private function myPrivateFunction() {
    // Only code inside MyClass can call this.
  }
}

A private member is fully usable inside the class — other methods of MyClass can read $this->myPrivateVariable and call $this->myPrivateFunction() — but any access from outside throws an Error.

Examples

Let's look at some practical examples of how the "private" keyword can be used:

Examples of PHP private

<?php

// Example 1
class Person
{
  private $name;

  public function __construct($name)
  {
    $this->name = $name;
  }

  private function getName()
  {
    return $this->name;
  }

  public function greet()
  {
    $name = $this->getName();
    echo "Hello, $name!" . PHP_EOL;
  }
}

$person = new Person("John");
$person->greet(); // Output: Hello, John!

// Example 2
class BankAccount
{
  private $balance = 0;

  public function deposit($amount)
  {
    $this->balance += $amount;
  }

  public function withdraw($amount)
  {
    if ($amount > $this->balance) {
      echo "Insufficient funds!" . PHP_EOL;
    } else {
      $this->balance -= $amount;
      echo "Withdrawal successful!" . PHP_EOL;
    }
  }
}

$account = new BankAccount();
$account->deposit(100);
$account->withdraw(50); // Output: Withdrawal successful!
$account->withdraw(100); // Output: Insufficient funds!

In both examples, $name, getName(), and $balance are part of the class's internal machinery. The outside world only touches greet(), deposit(), and withdraw() — the safe, validated entry points. That is encapsulation in action: the BankAccount balance can never go negative because nothing outside the class can write to it directly.

What happens when you break the rule

Accessing a private member from outside the class is a fatal Error, not a silent null:

<?php

class Person
{
  private $name = "John";
}

$p = new Person();
echo $p->name;
// PHP Fatal error: Uncaught Error: Cannot access private property Person::$name

This is intentional — it surfaces the mistake immediately rather than letting code quietly depend on internals.

Private is per-class, not per-object

A common surprise: private members are visible between two instances of the same class. The "private" boundary is the class, so one object can read another object's privates as long as both are the same type:

<?php

class Money
{
  private $cents;

  public function __construct($cents)
  {
    $this->cents = $cents;
  }

  // Reads $other->cents directly — allowed, same class.
  public function add(Money $other)
  {
    return new Money($this->cents + $other->cents);
  }

  public function format()
  {
    return number_format($this->cents / 100, 2);
  }
}

$total = (new Money(150))->add(new Money(350));
echo $total->format(); // Output: 5.00

Private and inheritance

Unlike protected, a private member is not inherited by subclasses. A child class cannot see the parent's private members, and if it declares a member of the same name it gets its own separate copy:

<?php

class Base
{
  private $value = "base";

  public function show()
  {
    echo $this->value . PHP_EOL;
  }
}

class Child extends Base
{
  private $value = "child";
}

(new Child())->show(); // Output: base

show() was defined in Base, so it sees Base's own private $value — the child's $value is a completely separate property. If you want subclasses to share and override a member, use protected instead. See PHP Inheritance for more.

When to use private

  • Internal state that must stay valid — like the BankAccount balance above. Expose mutators (deposit, withdraw) that enforce the rules.
  • Helper methods that are implementation details, so you can rename or rewrite them later without breaking callers.
  • Default to the most restrictive level. Start private; widen to protected or public only when a real need appears. It is far easier to loosen visibility than to tighten it after other code depends on it.

Benefits

  • Encapsulation — group data with the methods that operate on it and keep the rest of the program from reaching in. See PHP Classes and Objects.
  • Information hiding — callers depend on your public API, not your internals, so you can change how the class works without breaking them.
  • Invariants — because writes go through your methods, you can guarantee the object is always in a valid state.

Summary

The private keyword restricts a class member to the class that declares it — not its subclasses and not the outside world. It is the default tool for encapsulation: hide internal data and helpers, expose a small validated public API, and keep your objects in a consistent state. Reach for protected when subclasses need access, and public only for the intended interface.

Practice

Practice
What are the characteristics of Private functions in PHP?
What are the characteristics of Private functions in PHP?
Was this page helpful?