Coding/Learn/php/php interfaces and abstract classes
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

LearnInterfaces, Abstract Classes & Polymorphism
16 / 20 0%
Lesson 16 of 20
10 mins read

Interfaces, Abstract Classes & Polymorphism

PreviousNext Lesson
In Plain English

Build loosely-coupled, extensible architectures. Learn pure contract interfaces (interface, implements), partial implementations with abstract classes, multiple interface inheritance, and polymorphic dependency injection.

The Mental Model

"An interface is like a universal electrical wall outlet standard: any lamp, laptop charger, or blender that matches the 3-prong interface can plug in and draw power without caring how the power company generates the electricity."

Deep Dive: How It Works

Interfaces: Pure contracts declaring method signatures without implementations (e.g. interface Logger { public function log(string $msg): void; }).

Abstract Classes: Classes that cannot be instantiated directly and can contain both concrete methods and abstract method signatures.

Multiple Interfaces: A single class can implement multiple interfaces (class Repository implements Reader, Writer).

Polymorphism: Functions accept interface type hints, allowing arbitrary implementations to be passed interchangeable.

Syntax Blueprint

<?php

interface PaymentGateway {
    public function charge(float $amount): bool;
}

class StripeGateway implements PaymentGateway {
    public function charge(float $amount): bool { return true; }
}

Define contract with interface and implement all methods in concrete classes with implements.

PointCore Rules to Remember

Point star
Contract Enactment: PHP enforces that implementing classes must define all interface methods with matching signatures.
Point star
Dependency Inversion: High-level modules should depend on interfaces, not concrete classes.
Point star
Interface Constants: Interfaces can declare public constants accessible on the interface or implementing classes.

Common Beginner Traps & How to Fix Them

Mistake:Declaring non-public methods in an interface.

Why it happens: Interface methods are contracts by definition and must always be public.

How to fix: All methods declared inside an interface must be public.

Pro Tip: Keep interfaces focused and granular (Interface Segregation Principle): favor multiple small interfaces over one massive interface.

Live Interactive Example

Hit Run Code to see it live
Polymorphic Payment Gateways
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
Output Console
Click "Run Code" to view the rendered output.
How it works: checkout() polymorphically executed both gateways through the PaymentGateway interface contract.

Your Turn: Micro Challenge

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

Micro Exercise

Implement Renderable Interface

  • PointDefine an interface Renderable with public function render(): string;.
  • PointCreate a class Button implements Renderable with public function __construct(public string $label) {}.
  • PointImplement render() returning "<button>{$this->label}</button>".
  • PointIn main, create $btn = new Button("Submit") and print $btn->render().
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