PHP protected keyword tutorial shows how to use protected visibility in PHP. Learn protected access with practical examples.
last modified April 16, 2025
The PHP protected keyword is a visibility modifier used in object- oriented programming. It controls access to class members (properties and methods). Protected members are accessible within the class and its child classes.
The protected keyword makes class members available to the class itself and any classes that extend it. This is more restrictive than public but less restrictive than private visibility.
Protected members cannot be accessed from outside the class hierarchy. They are designed for internal class implementation details that child classes might need to access.
Syntax: protected $property; or protected function method() {}. The protected modifier can be applied to both properties and methods.
This example demonstrates a simple protected property in a parent class.
basic_protected.php
<?php
declare(strict_types=1);
class Vehicle { protected $model;
public function setModel(string $model): void {
$this->model = $model;
}
}
class Car extends Vehicle { public function getModel(): string { return $this->model; } }
$car = new Car(); $car->setModel(“Toyota”); echo $car->getModel();
The $model property is protected in the Vehicle class. The Car class can access it because it extends Vehicle. The property cannot be accessed directly from outside these classes.
This example shows how protected methods can be used in inheritance.
protected_method.php
<?php
declare(strict_types=1);
class Animal { protected function makeSound(): string { return “Some generic animal sound”; } }
class Dog extends Animal { public function bark(): string { return $this->makeSound() . " - Woof!"; } }
$dog = new Dog(); echo $dog->bark();
The makeSound method is protected in Animal. The Dog class can call this method internally. External code cannot call makeSound() directly. This enforces encapsulation while allowing inheritance.
This example demonstrates accessing protected members from a child class.
child_access.php
<?php
declare(strict_types=1);
class BankAccount { protected $balance = 0;
protected function deposit(int $amount): void {
$this->balance += $amount;
}
}
class SavingsAccount extends BankAccount { public function addMoney(int $amount): void { $this->deposit($amount); echo “New balance: " . $this->balance; } }
$account = new SavingsAccount(); $account->addMoney(100);
The SavingsAccount can access both the protected property $balance and method deposit from its parent. The public addMoney() method provides controlled access to these protected members.
This example shows a protected constructor used in a singleton pattern.
protected_constructor.php
<?php
declare(strict_types=1);
class Logger { protected function __construct() { echo “Logger initialized”; }
public static function getInstance(): self {
static $instance = null;
if (null === $instance) {
$instance = new static();
}
return $instance;
}
}
$logger = Logger::getInstance();
The constructor is protected to prevent direct instantiation. The static getInstance() method controls object creation. This is a common pattern for singleton classes.
This example demonstrates overriding protected methods in child classes.
method_override.php
<?php
declare(strict_types=1);
class Shape { protected function calculateArea(): float { return 0; }
public function getArea(): float {
return $this->calculateArea();
}
}
class Circle extends Shape { protected float $radius;
public function __construct(float $radius) {
$this->radius = $radius;
}
protected function calculateArea(): float {
return pi() * $this->radius * $this->radius;
}
}
$circle = new Circle(5); echo $circle->getArea();
The Circle class overrides the protected calculateArea method from Shape. The public getArea() method provides access to the calculation. This shows polymorphic behavior with protected methods.
This example shows protected properties used within traits.
trait_protected.php
<?php
declare(strict_types=1);
trait Loggable { protected $logMessages = [];
protected function addLog(string $message): void {
$this->logMessages[] = $message;
}
public function getLogs(): array {
return $this->logMessages;
}
}
class User { use Loggable;
public function login(): void {
$this->addLog("User logged in");
}
}
$user = new User(); $user->login(); print_r($user->getLogs());
The trait defines protected property $logMessages and method addLog. The User class using the trait can access these protected members. Traits provide horizontal code reuse with visibility control.
This example compares protected and private visibility.
protected_vs_private.php
<?php
declare(strict_types=1);
class ParentClass { private $privateVar = “private”; protected $protectedVar = “protected”; }
class ChildClass extends ParentClass { public function showProtected(): string { return $this->protectedVar; }
public function showPrivate(): string {
return $this->privateVar; // This will cause an error
}
}
$child = new ChildClass(); echo $child->showProtected(); // echo $child->showPrivate(); // Would cause fatal error
The child class can access the protected property but not the private one. This demonstrates the key difference between protected and private visibility. Protected allows inheritance access while private restricts to the defining class only.
Encapsulation: Use protected for implementation details that child classes need.
Documentation: Clearly document protected members for inheriting classes.
Access Control: Prefer protected over public for internal class APIs.
Testing: Test protected methods through public interfaces or reflection.
Design: Consider using protected abstract methods for template methods.
This tutorial covered PHP protected keyword with practical examples showing protected properties and methods in various object-oriented scenarios.
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all PHP tutorials.