0Pricing
PHP Academy · Lesson

Abstract Classes and Methods

Define partially implemented base classes with abstract keywords.

Abstract Classes and Methods is a free PHP Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Intro: Abstract Classes

An abstract class cannot be instantiated directly — it's a template. Abstract methods declare a signature with no implementation; subclasses must provide them.

Key Concepts

Key concepts:

  • Declared with abstract class
  • Abstract methods have no body: abstract public function x();
  • Subclasses must implement all abstract methods
  • Can contain both abstract and concrete methods

Abstract Class Definition

Example:

<?php
abstract class Animal {
    public function __construct(protected string $name) {}

    // Must be implemented by each subclass
    abstract public function sound(): string;

    // Shared concrete method
    public function introduce(): string {
        return "I am {$this->name} and I say: " . $this->sound();
    }
}

Implementing Abstract Methods

Continued:

<?php
class Dog extends Animal {
    public function sound(): string { return 'Woof'; }
}

class Cat extends Animal {
    public function sound(): string { return 'Meow'; }
}

$animals = [new Dog('Rex'), new Cat('Whiskers')];
foreach ($animals as $a) {
    echo $a->introduce() . PHP_EOL;
}
// I am Rex and I say: Woof
// I am Whiskers and I say: Meow

Template Method Pattern

More depth:

<?php
abstract class DataProcessor {
    // Template method: defines the algorithm skeleton
    final public function process(array $data): array {
        $data = $this->validate($data);
        $data = $this->transform($data);
        return $this->format($data);
    }

    abstract protected function validate(array $data): array;
    abstract protected function transform(array $data): array;

    protected function format(array $data): array { return $data; }
}

Abstract vs Interface

Practical use:

<?php
// Use abstract class when:
// - Subclasses share implementation code
// - You need constructor logic or state

// Use interface when:
// - Defining a contract (no implementation needed)
// - Multiple inheritance of type is needed

// A class can only extend ONE abstract class
// but can implement MULTIPLE interfaces

Partial Implementation

Advanced:

<?php
abstract class Repository {
    // Concrete method available to all subclasses
    protected function findById(int $id, string $table): array {
        $stmt = $this->getDb()->prepare("SELECT * FROM $table WHERE id = ?");
        $stmt->execute([$id]);
        return $stmt->fetch(PDO::FETCH_ASSOC) ?: [];
    }

    abstract protected function getDb(): PDO;
    abstract public function find(int $id): array;
}

Pattern

Common Abstract Classes patterns are used to solve recurring design problems cleanly. Apply them consistently for readable, maintainable code.

When to Use

Knowing when to apply Abstract Classes is as important as knowing how. Overuse leads to complexity; underuse leads to duplication.

Real-World Example

In real applications, Abstract Classes appears frequently in frameworks, ORMs, and service layers. Understanding it prepares you to contribute to professional PHP projects.

Common Pitfalls

Avoid these mistakes with Abstract Classes:

  • Abstract classes cannot be instantiated with new
  • All abstract methods must be implemented in concrete subclasses
  • Use the Template Method pattern to define algorithm skeletons
  • Abstract classes can have constructors, properties, and concrete methods

Quick Check

Can you instantiate an abstract class directly with 'new'?

Recap: Abstract Classes

Summary:

  • Abstract classes cannot be instantiated with new
  • All abstract methods must be implemented in concrete subclasses
  • Use the Template Method pattern to define algorithm skeletons
  • Abstract classes can have constructors, properties, and concrete methods

Frequently asked questions

Is the “Abstract Classes and Methods” lesson free?

Yes — the full text of “Abstract Classes and Methods” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.

What will I learn in “Abstract Classes and Methods”?

Define partially implemented base classes with abstract keywords. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start PHP Academy?

No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Abstract Classes and Methods” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this PHP Academy lesson?

Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Extending Classes with Inheritance
  2. Abstract Classes and Methods
  3. Implementing Interfaces
  4. Traits for Code Reuse
← Back to PHP Academy