ZetCode

PHP protected Keyword

last modified November 25, 2026

The PHP protected keyword makes a property, method, or constant visible to the class that declares it and to every class that extends it. It sits between public, which is open to all code, and private, which is open only to the declaring class.

This article shows protected properties and methods in use, the errors raised by access from outside, the template method pattern, a protected constructor, and the difference between protected and private state in a subclass.

Basic definitions

The protected keyword is a visibility modifier for properties, methods, and class constants. A protected member can be used inside the class that declares it and inside every class that extends it. Code outside the hierarchy cannot reach it.

PHP has three visibility levels. public members are reachable from anywhere, protected members are reachable from the class and its subclasses, and private members are reachable only from the declaring class. A method without a modifier is public, while an instance property needs a modifier or the older var keyword.

Protected is the level for members that are implementation details but are meant to be used or replaced by subclasses: hooks called from a public method, state that a child must be able to update, or a constructor that only subclasses may call. The keyword is a design boundary, not a security boundary, because reflection can still read the members.

A protected property

The first example stores the model of a vehicle in a protected property. The setter is public, so code outside the hierarchy still controls the value.

prot_property.php
<?php

declare(strict_types=1);

class Vehicle
{
    protected string $model = "unknown";

    public function setModel(string $model): void
    {
        $this->model = $model;
    }
}

class Car extends Vehicle
{
    public function describe(): string
    {
        return "Car model: " . $this->model;
    }
}

$car = new Car();
$car->setModel("Toyota");
echo $car->describe(), "\n";

The $model property belongs to Vehicle, and Car reads it from its own method because it extends the class. Code outside the hierarchy cannot touch the property at all. Keeping the property protected and the setter public means every change still goes through a method where a check can be added later.

$ php prot_property.php
Car model: Toyota

Access from outside is an error

An attempt to read a protected property, call a protected method, or read a protected constant from outside the hierarchy throws an Error.

prot_outside.php
<?php

declare(strict_types=1);

class Account
{
    protected float $balance = 100.0;
    protected const KIND = "account";

    protected function audit(): string
    {
        return "audit";
    }
}

$account = new Account();

try {
    echo $account->balance, "\n";
} catch (Error $e) {
    echo get_class($e), ": ", $e->getMessage(), "\n";
}

try {
    $account->audit();
} catch (Error $e) {
    echo get_class($e), ": ", $e->getMessage(), "\n";
}

try {
    echo Account::KIND, "\n";
} catch (Error $e) {
    echo get_class($e), ": ", $e->getMessage(), "\n";
}

The messages name the class and the member: Cannot access protected property Account::$balance, Call to protected method Account::audit() from global scope, and Cannot access protected constant Account::KIND. All three are instances of Error, which can be caught for diagnostics; in normal code they point to a programming mistake, so the call should be fixed rather than wrapped in a catch block.

$ php prot_outside.php
Error: Cannot access protected property Account::$balance
Error: Call to protected method Account::audit() from global scope
Error: Cannot access protected constant Account::KIND

The template method pattern

A public method can define the outline of an operation and call protected hooks that subclasses fill in. The pattern is called a template method, and it is the most common reason to declare a method protected.

prot_template.php
<?php

declare(strict_types=1);

abstract class Report
{
    public function render(): string
    {
        return $this->title() . "\n" . $this->body();
    }

    abstract protected function title(): string;

    protected function body(): string
    {
        return "No content";
    }
}

class SalesReport extends Report
{
    protected function title(): string
    {
        return "Sales";
    }

    protected function body(): string
    {
        return "Total: 1200";
    }
}

echo (new SalesReport())->render(), "\n";

Report::render is public and always assembles the title and the body the same way. The two hooks are protected, and title is abstract, so every concrete report must supply it. SalesReport overrides both hooks, and render picks up the overrides through ordinary polymorphism. The caller cannot call the hooks directly, which keeps the assembly rule in one place.

$ php prot_template.php
Sales
Total: 1200

A protected constructor

A protected constructor prevents new from outside the hierarchy. The class then decides which subclasses may be created and through which factory method.

prot_ctor.php
<?php

declare(strict_types=1);

class Model
{
    protected function __construct(protected string $name)
    {
    }
}

class User extends Model
{
    public static function create(string $name): self
    {
        return new self($name);
    }

    public function getName(): string
    {
        return $this->name;
    }
}

try {
    new Model("x");
} catch (Error $e) {
    echo get_class($e), ": ", $e->getMessage(), "\n";
}

echo User::create("Alice")->getName(), "\n";

Creating a Model directly fails with Call to protected Model::__construct() from global scope. User::create succeeds because it runs inside a subclass, where the constructor is visible. A protected constructor is useful for a base class that only makes sense as part of a hierarchy, or for a class that builds its instances through named constructors rather than new.

$ php prot_ctor.php
Error: Call to protected Model::__construct() from global scope
Alice

Protected and private in a subclass

A subclass can read a protected property of its parent, but not a private one. The next example declares both in the parent and redeclares both in the child.

prot_private.php
<?php

declare(strict_types=1);

class Base
{
    protected string $role = "base";
    private string $secret = "base-secret";

    public function info(): string
    {
        return $this->role . " / " . $this->secret;
    }
}

class Child extends Base
{
    protected string $role = "child";
    private string $secret = "child-secret";

    public function own(): string
    {
        return $this->role . " / " . $this->secret;
    }
}

$child = new Child();
echo $child->info(), "\n";
echo $child->own(), "\n";

Both methods read $role as child, because a protected property is one storage slot shared by the hierarchy, and the child's default value initialized that slot. The two $secret properties are different slots: Base::info reads the private property of Base and Child::own reads the private property of Child. This is the practical difference between the two levels: protected state can be changed by a subclass, private state belongs to the class that declared it.

$ php prot_private.php
child / base-secret
child / child-secret

Protected members of other instances

Visibility is checked per class, not per object. A method may read the protected members of any object in its own hierarchy, not only of $this.

prot_instances.php
<?php

declare(strict_types=1);

class Money
{
    public function __construct(protected int $amount)
    {
    }

    public function add(Money $other): Money
    {
        return new Money($this->amount + $other->amount);
    }

    public function getAmount(): int
    {
        return $this->amount;
    }
}

$total = (new Money(25))->add(new Money(17));
echo $total->getAmount(), "\n";

Money::add receives another Money and reads $other->amount, although the property is protected. PHP allows it because the code runs inside Money. The same rule supports comparing, merging, and copying objects without exposing their fields; an unrelated class that tried the same read would get an Error.

$ php prot_instances.php
42

Visibility at a glance

The table summarizes where each visibility level is usable.

Scopepublicprotectedprivate
Inside the declaring classyesyesyes
Inside a subclassyesyesno
Outside the class hierarchyyesnono

A subclass may widen a protected method to public, but it may not narrow a public method to protected. The other two levels are covered in PHP private keyword and PHP public keyword.

Best practices

Protected members are part of the contract with subclasses, so every one of them limits how the class can change later.

Source

PHP manual: visibility

PHP manual: inheritance

PHP manual: class constants

This article covered the PHP protected keyword: protected properties, methods, and constants, access errors, the template method pattern, protected constructors, other instances in the hierarchy, and the visibility table. Continue with PHP public keyword for the open level, or review PHP private keyword for the strict one.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all PHP tutorials.