W3docs

protected

The "protected" keyword is used in PHP to declare a class member as protected, meaning that it can only be accessed within the class itself and its subclasses.

The PHP protected Keyword

protected is one of PHP's three access modifiers (alongside public and private). It controls visibility — who is allowed to read or call a class member.

A member declared protected can be accessed:

  • inside the class that declares it, and
  • inside any class that inherits from it (a subclass).

It cannot be accessed from "outside" — that is, from regular code that just holds an object ($obj->member). This middle ground is exactly what makes protected useful: it hides implementation details from the outside world while still letting subclasses build on top of them. For the full comparison, see PHP Access Modifiers.

This page covers the syntax, how protected behaves across inheritance, the common gotchas, and when to reach for it instead of private or public.

Syntax

Put the protected keyword in front of a property or method declaration:

class MyClass {
  protected $myProtectedProperty;

  protected function myProtectedMethod() {
    // Code here
  }
}

You can also mark a static member as protected:

class Config {
  protected static $instances = 0;

  protected static function register() {
    self::$instances++;
  }
}

protected vs private vs public

The three modifiers differ only in where a member is visible:

ModifierSame classSubclassOutside code
publicyesyesyes
protectedyesyesno
privateyesnono

The key distinction: private members are invisible even to subclasses, while protected members are shared down the inheritance chain. Choose protected when a subclass legitimately needs the data or helper, but you still want to keep it out of the public API.

Examples

The examples below show protected members being used by a subclass (allowed) — then we'll look at what happens when outside code tries the same thing.

<?php

// Example 1
class Animal
{
  protected $name;

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

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

class Dog extends Animal
{
  public function bark()
  {
    $name = $this->getName();
    echo "$name barks!" . PHP_EOL;
  }
}

$dog = new Dog("Rufus");
$dog->bark(); // Output: Rufus barks!

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

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

  protected function canWithdraw($amount)
  {
    return $amount <= $this->balance;
  }
}

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

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

In both classes the subclass (Dog, SavingsAccount) freely calls the protected member of its parent. The data and helper logic stay reusable through inheritance without ever being exposed to outside code.

Accessing protected members from outside fails

The moment you try to touch a protected member from regular code, PHP throws a fatal error:

<?php

class Animal {
  protected $name = "Rufus";
}

$animal = new Animal();
echo $animal->name; // Fatal error: Cannot access protected property Animal::$name

This is the protection in action — the property simply isn't part of the object's public surface.

Gotcha: a subclass can reach siblings of the same type

A common surprise is that visibility is checked by class, not by individual object. An object can access the protected members of another object as long as both are instances of the same class (or a related class that can see the member):

<?php

class Wallet {
  protected $balance = 100;

  public function isRicherThan(Wallet $other): bool
  {
    // $other->balance is protected, but we're inside Wallet, so it's allowed
    return $this->balance > $other->balance;
  }
}

$a = new Wallet();
$b = new Wallet();
var_dump($a->isRicherThan($b)); // bool(false)

When to use protected

Reach for protected when:

  • A subclass needs to reuse or override internal state or helper methods, but outside code should not.
  • You are designing a base class meant to be extended (see PHP Abstract Classes), and you want to offer "building block" methods to subclasses only.

Prefer private when even subclasses should not depend on an internal detail — that gives you the freedom to change it later without breaking child classes. Prefer public only for the intentional, documented API of the class.

Summary

  • protected members are visible inside the declaring class and its subclasses, but not to outside code.
  • It is the middle ground between public (visible everywhere) and private (visible only in the declaring class).
  • Visibility is enforced per class, so same-type objects can see each other's protected members.
  • Use it to share reusable internals down an inheritance chain while keeping them out of the public API.

To go deeper, continue with PHP Inheritance, PHP Classes and Objects, and Static Properties.

Practice

Practice
In PHP, which of the following can access protected properties and methods?
In PHP, which of the following can access protected properties and methods?
Was this page helpful?