Coding/Learn/php/php inheritance and access modifiers
PHP Foundational Course
01.Introduction, Server-Side Architecture & Syntax
02.Variables, Strict Types & Dynamic Casting
03.Strings, Numbers & Built-in Math Functions
04.Operators, Null Coalescing & Magic Constants
05.Control Flow: If-Else, Switch & Modern Match Expressions
06.Loops: While, For & Foreach Traversal
07.Functions, Type Declarations & Named Arguments
08.Arrays & Functional Array Manipulation
09.Superglobals, Form Handling & Input Sanitization
10.Date, Time & DateTimeImmutable Objects
11.File System, Streams & I/O Operations
12.Cookies, Sessions & State Management
13.Exceptions, Throwables & Custom Error Fences
14.Classes, Objects & Constructor Property Promotion
15.Inheritance, Access Modifiers & Method Overriding
16.Interfaces, Abstract Classes & Polymorphism
17.Traits, Static Members & Namespaces
18.PDO, MySQL & Prepared Statements against SQL Injection
19.JSON & XML REST API Interoperability
20.Capstone: Production Secure Backend API & User Registry Engine
Course Certificate0%

Complete all lessons to earn

LearnInheritance, Access Modifiers & Method Overriding
15 / 20 0%
Lesson 15 of 20
9 mins read

Inheritance, Access Modifiers & Method Overriding

PreviousNext Lesson
In Plain English

Organize object hierarchies through inheritance. Master encapsulation with access modifiers (public, protected, private), extend base classes with extends, invoke parent methods with parent::, and prevent extension using the final keyword.

The Mental Model

"Access modifiers are like security clearances in a corporate headquarters: public is the open lobby, protected is the employee-only workspace shared with sister branches (subclasses), and private is the CEO personal locked vault."

Deep Dive: How It Works

Access Modifiers: public (everywhere), protected (class & subclasses only), private (declaring class only).

Inheritance: class Manager extends Employee inherits all non-private methods and properties.

Parent Invocation: parent::__construct(...) or parent::doWork() calls overridden base implementations.

Final Classes & Methods: final prevents other classes from extending or overriding critical security routines.

Syntax Blueprint

<?php

class BaseAccount {
    protected float $balance = 0;
}

class SavingsAccount extends BaseAccount {
    public function addInterest() { $this->balance *= 1.05; }
}

Use protected for shared subclass state and extends to inherit base class behavior.

PointCore Rules to Remember

Point star
Encapsulation: Keep internal state private or protected, exposing only intentional public API methods.
Point star
Method Overriding: Subclasses can override parent methods to specialize behavior while calling parent:: when needed.
Point star
Liskov Substitution: Subclasses must maintain compatible parameter and return types with the parent signature.

Common Beginner Traps & How to Fix Them

Mistake:Trying to access private properties from a subclass ($this->privateField).

Why it happens: private properties are strictly restricted to the declaring class.

How to fix: Change modifier to protected if subclasses legitimately need access.

Pro Tip: Mark utility classes and value objects as final class to enforce composition over fragile deep inheritance trees.

Live Interactive Example

Hit Run Code to see it live
Inheritance and Parent Constructor Chaining
PHP 8.2 / CLI
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Output Console
Click "Run Code" to view the rendered output.
How it works: Manager inherited getDetails() and accessed protected $salary in getTotalComp().

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Extend Base Notification Class

  • PointCreate a class Notification with public function send(string $to): string returning "Notification sent to {$to}".
  • PointCreate a subclass EmailNotification extends Notification that overrides send(string $to): string to return "Email dispatched to {$to}".
  • PointIn main script, create $email = new EmailNotification() and print the result of $email->send("[email protected]").
1
2
3
4
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates