CodeBlanks Logo Icon CodeBlanks

Summary - Abstract classes

PHP course · Intermediate · Lesson 58/115

Build a data source system with abstract class, abstract method and template method.

Summary - Abstract classes

In this group you learned five core concepts of abstract classes:

  • abstract class for template classes (cannot be instantiated with new)
  • abstract method for bodyless signature declarations
  • Inheritance with extends and implementation of abstract methods
  • Template method pattern: the parent sets the skeleton, subclasses fill parts
  • Abstract constructor + parent::__construct for shared init

You will combine them in a data source system: the DataSource abstract class declares fetch(), subclasses bring data; the load() template method runs the whole workflow.

Exercise Data source system

Define a DataSource abstract class with a protected string $name property. Its constructor public function __construct(string $name) assigns $this->name = $name. Declare an abstract method fetch(): array. The template method public function load(): string should fetch data with $rows = $this->fetch(); and return $this->name . ": " . count($rows) . " records";. Then derive FileSource from DataSource; its fetch() method returns return ["a", "b", "c"];. Use $src = new FileSource("user data") and echo $src->load() . "\n";

Show example solution
array and a load() method. Inside load() fetch data with $rows = $this->fetch() and count with count($rows). The FileSource subclass implements fetch().
Example solution:
<?php
// Define the DataSource abstract class
abstract class DataSource {
    protected string $name;

    public function __construct(string $name) {
        $this->name = $name;
    }

    abstract public function fetch(): array;

    public function load(): string {
        $rows = $this->fetch();
        return $this->name . ": " . count($rows) . " records";
    }
}

class FileSource extends DataSource {
    public function fetch(): array {
        return ["a", "b", "c"];
    }
}

$src = new FileSource("user data");
echo $src->load() . "\n";

Tips for abstract classes

Abstract classes are the backbone of inheritance hierarchies. Remember:

  • An abstract class cannot be instantiated with new; only inherited with extends.
  • An abstract method is declared without a body; the subclass implements it with the same signature.
  • It can contain concrete methods and properties; these are inherited.
  • The template method pattern keeps the workflow in one place.
  • If the subclass defines a constructor, parent::__construct must be called explicitly.

In the next group we'll look at traits for code reuse.

Test yourself

  1. Match all abstract class concepts

    abstract class
    Template, no new instance
    abstract method
    Bodyless signature
    extends
    Inherits abstract class
    template method
    Sets the skeleton
    parent::__construct
    Calls the constructor
  2. Which statement about abstract classes is true?

    • It can contain both concrete and abstract methods; it cannot be instantiated with new
    • It can only contain abstract methods
    • It can extend multiple classes at once
    • When used instead of an interface, implements is required
    Show answer

    It can contain both concrete and abstract methods; it cannot be instantiated with new — Correct. An abstract class can contain both concrete and abstract methods and cannot be instantiated with new.

All lessons in Intermediate