CodeBlanks Logo Icon CodeBlanks

protected property and method

PHP course · Intermediate · Lesson 16/115

Learn that protected access is only for the class and its subclasses.

The protected access level

PHP has three access levels: public, protected and private. They control where a property or method can be accessed from.

public is accessible everywhere. private only inside the defining class. protected inside the defining class AND its subclasses (children).

If you mark a property protected, child classes can access and modify it directly with $this->.... This is the most common level when you plan to use inheritance.

A protected inheritance example

A BankAccount class might want to protect its $balance property from direct modification. If you make it private, the child SavingsAccount cannot access this property and cannot add interest.

If you make it protected instead, the SavingsAccount::applyInterest() method can write $this->balance += $amount. This gives access that is closed to the outside world but open to children.

This balance is the key to building secure and extensible class hierarchies.

Exercise SavingsAccount uses the protected balance

In this lesson the BankAccount class has a $balance property declared protected. You will derive SavingsAccount from BankAccount and write an applyInterest(int $amount) method. Inside this method, add interest to the protected balance with $this->balance += $amount. Create a SavingsAccount instance with a starting balance of 1000, add 50 interest, then print the getBalance() output.

Show example solution
SavingsAccount extends BankAccount. The applyInterest method writes $this->balance += $amount; it can access balance because it's protected.
Example solution:
<?php

// Bank account class
class BankAccount {
    protected int $balance;

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

    public function getBalance(): int {
        return $this->balance;
    }
}

// Savings account class
class SavingsAccount extends BankAccount {
    public function applyInterest(int $amount): void {
        $this->balance += $amount;
    }
}

$account = new SavingsAccount(1000);
$account->applyInterest(50);
echo $account->getBalance();

Test yourself

  1. From where can a protected property be accessed?

    • Only from outside the defining class
    • From everywhere
    • From the defining class and its subclasses
    • From nowhere
    Show answer

    From the defining class and its subclasses — Correct; a protected member is accessible from the class and its subclasses.

  2. Complete the protected declaration

        ____ int $balance;
    
        public function __construct(int $balance) {
            $this->balance = $balance;
        }
    Show answer
        protected int $balance;
    
        public function __construct(int $balance) {
            $this->balance = $balance;
        }
  3. Match access levels

    public
    Accessible everywhere
    protected
    Class and subclasses
    private
    Only inside the class

All lessons in Intermediate