Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🔴 Advanced  ·  Lesson 24

Abstract Class vs Interface in PHP

In this lesson: Difference between abstract class and interface in PHP — rules, comparison table and real examples to choose the right one.

Defining a contract

Sometimes you want to say "any class of this kind must have these methods". PHP offers two tools for this: an abstract class and an interface. Both set rules that other classes must follow, but they differ in how much they provide. Choosing the right one keeps your code well-organised.

💡 Abstract class = a partial base you extend. Interface = a pure list of methods you implement.

Abstract class

An abstract class can have real methods (with code) and abstract methods (just the name). You cannot create an object from it directly — a child class fills in the blanks.

<?php
  abstract class Shape {
      abstract public function area();      // child must define
      public function describe() {          // shared code
          return "Area is " . $this->area();
      }
  }
  class Square extends Shape {
      public $side = 4;
      public function area() { return $this->side * $this->side; }
  }
  echo (new Square())->describe();
?>
Area is 16

Square must define area(), but reuses the finished describe() from the abstract base.

Interface

An interface only lists method names — no code at all. A class implements it and must provide every method.

<?php
  interface Printable {
      public function printMe();
  }
  class Invoice implements Printable {
      public function printMe() { return "Printing invoice"; }
  }
  echo (new Invoice())->printMe();
?>
Printing invoice

The interface guarantees Invoice has a printMe() method, but provides no code itself. A class can implement many interfaces.

Key differences

PointAbstract classInterface
Method codeCan have someNone (just names)
How many per classExtend only oneImplement many
Keywordextendsimplements
Best forShared base + blanksPure contract

When to use which

Use an abstract class when related classes share code and each must fill in a few methods — like different shapes that all describe themselves but compute area differently. Use an interface when unrelated classes just need to support the same action — like anything that can be "printed" or "saved", with no shared code.

Common mistakes

  • Trying to create an object of an abstract class. You must extend it with a child class first.
  • Putting code in an interface. Interfaces only declare method names, not bodies.
  • Extending several classes. A class extends only one; use interfaces for multiple contracts.
  • Not implementing every interface method. A class must define all methods the interface lists.

Summary

  • Both set rules other classes must follow.
  • An abstract class can share code and has abstract methods to fill in.
  • An interface only lists method names, no code.
  • Extend one abstract class; implement many interfaces.
  • Abstract = shared base; interface = pure contract.

Next, reuse code across classes with PHP traits, or review the basics in PHP OOP.

Frequently Asked Questions

What is the difference between an abstract class and an interface?
An abstract class can have both finished methods (with code) and abstract methods (just the name), and a class can extend only one. An interface only lists method names with no code, and a class can implement many interfaces. Abstract = partial base; interface = pure contract.
When should I use an abstract class?
Use an abstract class when related classes share common code you want to reuse, plus some methods each child must fill in. It gives a base with shared behaviour and required blanks.
When should I use an interface?
Use an interface when you only want to guarantee that a class has certain methods, without sharing any code. It is ideal when unrelated classes must all support the same actions, like different classes that can all be "printable".
Can a class use both an abstract class and interfaces?
Yes. A class can extends one abstract class and implements several interfaces at the same time. This combines shared base code with multiple contracts.
Why can a class implement many interfaces but extend only one class?
Extending one class keeps inheritance simple and avoids conflicts from two base implementations. Interfaces carry no code, so implementing many causes no conflict — a class can promise to fulfil several contracts safely.
← Back to PHP Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicMagic Methods Your progress: 24 / 50 (48%) · Lesson 25 / 50 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free