I think it's important to, first, clarify terminology, in order to more elaborately answer this question.

  1. inheritance
    • Inheritance is actually broadly applied to a lot of Object-Oriented programming principles and concepts. It just entails one thing bred from another. So whether you are implementing an interface or extending a class you are still using a form of inheritance. They aren't mutually exclusive concepts.
  2. interface
    • Try to think of an interface like you would a contract. The contract itself is just a document, usually between two or more parties, that lays out the rules of their relationship. Interfaces, specifically in the context of OOP and PHP, do not provide implementation. They only provide the required public methods that an implementing class MUST implement. Interfaces also cannot be instantiated on their own.
  3. abstract class
    • The abstract class is similar to an interface in that it cannot be instantiated on its own, but does not necessarily enforce a contract on the extending class. Since it's an actual class, and not just an interface, it also allows for implementation. This implementation can be supplied by the abstract class itself, or left up to the extending class, if the method is declared as abstract in the abstract class. It also allows for the implementation of properties and private/protected methods, because the inheritance here acts like a base class and not just a requirement.

So to answer the question, "why do we have abstract classes in PHP", because it's useful. You may see these as intractable ideas at first, but they actually can work together to provide conjoined utility.

Example

Consider that some times an interface isn't enough to create a useful implementation. The interface can only enforce that a method exists and that its signature is compatible with the implemented interface. There may be cases when you wish to provide default implementations of an interface, for example.

interface Device {
    public function input(Stream $in);
    public function output(): Stream;
}

abstract class DefaultDevice implements Device {
    protected $buffer = "";
    public function input(Stream $in) {
        $this->buffer .= $in->read(1024);
        $this->process();
    }
    abstract protected function process();
}

So now any class that extends DefaultDevice can either choose to override the implementation of the input method or not. It also has to implement a process method even though the interface does not require it. This means other classes implementing the Device interface can be backwards compatible and this remains an implementation detail.

Further Example

Separating implementation from specification is generally a key attribute of well-written software.

Take a look at the Device interface itself, as a good example. We rely on the input method to accept a Stream type and the output method to return a Stream type. Since Stream, itself, can actually be an interface this means that any type implementing Stream is acceptable. So I could create my own class and implement the Stream interface without ever breaking this code.

class CustomStream implements Stream {
    public function read($bytes = 1024) {
        /* implementation */
    }
    public function write($data) {
        /* implementation */
    }
}

$device->input(new CustomStream); // this will not throw an error
Answer from Sherif on Stack Overflow
๐ŸŒ
PHP
php.net โ€บ manual โ€บ en โ€บ language.oop5.abstract.php
PHP: Class Abstraction - Manual
PHP has abstract classes, methods, and properties. Classes defined as abstract cannot be instantiated, and any class that contains at least one abstract method or property must also be abstract.
๐ŸŒ
W3Schools
w3schools.com โ€บ php โ€บ php_oop_classes_abstract.asp
PHP OOP Abstract Classes
The child class method must be defined with the same name and it redeclares the parent abstract method ยท The child class method must be defined with the same or a less restricted access modifier ยท The number of required arguments must be the same. However, the child class may have optional arguments in addition ... <?php // Parent class abstract class Car { public $name; public function __construct($name) { $this->name = $name; } abstract public function intro() : string; } // Child classes class Audi extends Car { public function intro() : string { return "Choose German quality!
Discussions

PHP Abstract Classes, PHP Interfaces
An interface is simply a template for building a class. It does not contain code inside functions. It merely defines which need to appear in implemented classes. It's generally used to ensure all classes of a certain type obey a common naming convention. Imagine you need to connect a database. The interface defines the functions classes can use to make database calls, regardless of the database. So even if the database changes (say from MySQL to PostgreSQL), it doesn't matter. The function names will be the same, and the data returned with be the same. https://www.php.net/manual/en/language.oop5.interfaces.php interface Template { public function doSomething(int $id): string public function doAnotherThing(string $email): bool } class myClass implements Template { public function doSomething(int $id): string { // Do something and return a string } public function doAnotherThing(string $email): bool { // Do something and return a boolean (true / false) } } class mySecondClass implements Template { public function doSomething(int $id): string { // Do something else and return a string } public function doAnotherThing(string $email): bool { // Do something else and return a boolean (true / false) } } More on reddit.com
๐ŸŒ r/PHPhelp
9
8
November 17, 2022
abstraction - Why are abstract classes necessary in PHP? - Stack Overflow
We can use simple inheritance or interface instead of abstraction. Why do we need to use abstraction in PHP? and How can we hide basic features using abstraction? I am confused using abstraction and More on stackoverflow.com
๐ŸŒ stackoverflow.com
php - Is it good practice to declare and set properties in abstract classes? - Software Engineering Stack Exchange
I am using PHP and designing some abstract classes. I can declare properties with constant values within my abstract class and access/overwrite them from any class which extends this without re More on softwareengineering.stackexchange.com
๐ŸŒ softwareengineering.stackexchange.com
April 22, 2014
Creating abstract properties in PHP - PHP - SitePoint Forums | Web Development & Design Community
Hi I was just wondering if it was possible to create/emulate abstract properties in PHP. This is for a Page class, which is extended for each page in the site. A skeleton in shown below: abstract class Page { proโ€ฆ More on sitepoint.com
๐ŸŒ sitepoint.com
0
March 15, 2009
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ abstract-classes-in-php
Abstract Classes in PHP - GeeksforGeeks
March 15, 2025 - Abstract classes in PHP are classes that may contain at least one abstract method. Unlike C++, abstract classes in PHP are declared using the abstract keyword. The purpose of abstract classes is to enforce that all derived classes implement ...
๐ŸŒ
Reddit
reddit.com โ€บ r/phphelp โ€บ php abstract classes, php interfaces
r/PHPhelp on Reddit: PHP Abstract Classes, PHP Interfaces
November 17, 2022 -

I read about it online and still can't understand what these two is about. How does it work and when do you use these two? Can someone give example and explain these to me in simple words? Thanks!

Top answer
1 of 5
10
An interface is simply a template for building a class. It does not contain code inside functions. It merely defines which need to appear in implemented classes. It's generally used to ensure all classes of a certain type obey a common naming convention. Imagine you need to connect a database. The interface defines the functions classes can use to make database calls, regardless of the database. So even if the database changes (say from MySQL to PostgreSQL), it doesn't matter. The function names will be the same, and the data returned with be the same. https://www.php.net/manual/en/language.oop5.interfaces.php interface Template { public function doSomething(int $id): string public function doAnotherThing(string $email): bool } class myClass implements Template { public function doSomething(int $id): string { // Do something and return a string } public function doAnotherThing(string $email): bool { // Do something and return a boolean (true / false) } } class mySecondClass implements Template { public function doSomething(int $id): string { // Do something else and return a string } public function doAnotherThing(string $email): bool { // Do something else and return a boolean (true / false) } }
2 of 5
2
Abstract classes are useful when every implementation of them (every sub-class) shares some common behaviour. It saves you having to code the same or similar logic in every implementation. Interfaces define what methods a class needs to have, and it means in dependent code where you don't care what kind of implementation you get, you can type hint for the interface and you know what methods are available.
Top answer
1 of 3
12

I think it's important to, first, clarify terminology, in order to more elaborately answer this question.

  1. inheritance
    • Inheritance is actually broadly applied to a lot of Object-Oriented programming principles and concepts. It just entails one thing bred from another. So whether you are implementing an interface or extending a class you are still using a form of inheritance. They aren't mutually exclusive concepts.
  2. interface
    • Try to think of an interface like you would a contract. The contract itself is just a document, usually between two or more parties, that lays out the rules of their relationship. Interfaces, specifically in the context of OOP and PHP, do not provide implementation. They only provide the required public methods that an implementing class MUST implement. Interfaces also cannot be instantiated on their own.
  3. abstract class
    • The abstract class is similar to an interface in that it cannot be instantiated on its own, but does not necessarily enforce a contract on the extending class. Since it's an actual class, and not just an interface, it also allows for implementation. This implementation can be supplied by the abstract class itself, or left up to the extending class, if the method is declared as abstract in the abstract class. It also allows for the implementation of properties and private/protected methods, because the inheritance here acts like a base class and not just a requirement.

So to answer the question, "why do we have abstract classes in PHP", because it's useful. You may see these as intractable ideas at first, but they actually can work together to provide conjoined utility.

Example

Consider that some times an interface isn't enough to create a useful implementation. The interface can only enforce that a method exists and that its signature is compatible with the implemented interface. There may be cases when you wish to provide default implementations of an interface, for example.

interface Device {
    public function input(Stream $in);
    public function output(): Stream;
}

abstract class DefaultDevice implements Device {
    protected $buffer = "";
    public function input(Stream $in) {
        $this->buffer .= $in->read(1024);
        $this->process();
    }
    abstract protected function process();
}

So now any class that extends DefaultDevice can either choose to override the implementation of the input method or not. It also has to implement a process method even though the interface does not require it. This means other classes implementing the Device interface can be backwards compatible and this remains an implementation detail.

Further Example

Separating implementation from specification is generally a key attribute of well-written software.

Take a look at the Device interface itself, as a good example. We rely on the input method to accept a Stream type and the output method to return a Stream type. Since Stream, itself, can actually be an interface this means that any type implementing Stream is acceptable. So I could create my own class and implement the Stream interface without ever breaking this code.

class CustomStream implements Stream {
    public function read($bytes = 1024) {
        /* implementation */
    }
    public function write($data) {
        /* implementation */
    }
}

$device->input(new CustomStream); // this will not throw an error
2 of 3
2

an abstract class is used to provide a set of data members or methods to be made available to classes which inherit from it, even though the base class is not particularly useful (and should never be instantiated on its own) without an inherited implementation.

from here, inheritance takes over.

an interface on the other hand is to provide a set of rules for implementation that require each class that uses the interface to implement the specifications found therein. classes implementing the same interface do not need to inherit from each other, they implement the interface so they can be used in any application requiring that set of functionality.

๐ŸŒ
Medium
medium.com โ€บ @Amir_M4A โ€บ in-the-world-of-php-programming-there-are-several-key-concepts-that-developers-need-to-understand-b2ed1916287f
PHP: Abstract Classes, Interfaces, and Traits Explained | Medium
September 23, 2024 - An abstract class in PHP is a class that cannot be instantiated and is meant to serve as a blueprint for other classes to inherit from. It contains abstract methods, which are declared but not implemented within the abstract class itself.
Find elsewhere
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ php โ€บ php_abstract_classes.htm
PHP - Abstract Classes
An abstract class in PHP is a class that cannot be created on its own. This means you can't create objects straight from an abstract class. Abstract classes are intended to be extended by subsequent classes. They serve as a blueprint for other classes, defining the common methods and properties that
๐ŸŒ
DEV Community
dev.to โ€บ patricia1988hernandez2 โ€บ abstract-classes-in-php-what-they-are-and-how-to-use-them-20cp
Abstract Classes in PHP: What They Are and How to Use Them - DEV Community
April 22, 2025 - PHP abstract classes help you define a base class that other classes must follow. They act like a blueprint. You can't create an object from an abstract class directly.
๐ŸŒ
dailycomputerscience
dailycomputerscience.com โ€บ post โ€บ php-oop-fundamentals-abstract-classes-in-php
PHP OOP Fundamentals - Abstract Classes in PHP
October 11, 2024 - This ensures consistency across related classes while allowing some flexibility in how subclasses implement the details. ... In PHP, abstract classes are defined using the abstract keyword.
๐ŸŒ
CodeSignal
codesignal.com โ€บ learn โ€บ courses โ€บ revisiting-oop-concepts-in-php โ€บ lessons โ€บ understanding-abstract-classes-and-abstract-methods-in-php
Understanding Abstract Classes and Abstract Methods
Abstract classes and abstract methods are essential when you want to define a common interface for a group of derived classes. They ensure that derived classes implement specific methods, enabling you to write more robust and scalable programs. Let's revisit some of the key concepts through the following PHP ...
๐ŸŒ
Cleancode
cleancode.studio โ€บ series โ€บ object-oriented-php โ€บ episodes โ€บ 4
Clean Code Studio - PHP Abstract Classes
Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class). Abstract method: can only be used in an abstract class, and it does not have a body.
๐ŸŒ
Owlbuddy
owlbuddy.com โ€บ home โ€บ php tutorials โ€บ php oop's โ€บ abstract class in php
Unraveling Abstract Class in PHP: A Comprehensive Guide
December 12, 2023 - An abstract class in PHP serves as a blueprint for other classes and cannot be instantiated on its own. It provides a foundation for its child classes to inherit, defining common attributes and method signatures.
Top answer
1 of 4
2

This may be a personal preference, but I avoid having base classes for the sake of sharing properties. Particularly in data classes. I don't mind the repetition, and I avoid locking my classes into a fixed "data" hierarchy.

There are enough problems with inheritance, particularly over time as the code base grows, that I am reluctant to share methods through a class hierarchy, and even more reluctant to share properties through a class hierarchy.

...

Hm. I just noticed that you mentioned "static" properties. Rather than static (i.e. global) properties in a base class, you're better off with a separate, single instance of something dispensed by a factory. Make sure you provide for concurrency to avoid race conditions.

2 of 4
1

To answer your question, yes. If the abstract class is the logical place for the property then put it there. Use the tools that classes and OOP gives you.

"overtime the original structure/layout of these abstract properties is forgotten and could be left at there default static values or even forgotten(not used) completely."

This implies to me that developers using/creating the abstract class or child classes are required to do something with these properties. If that is true, try to come up with a class design that forces a developer to deal with this. That's better then to trust everyone to always do it correctly.

You mention changing static values. Static properties are shared between all class instances, so by changing their value in one object you change it for all. Multiple objects changing global state is something you want to avoid, because it is difficult to keep all changes in the correct order.

๐ŸŒ
Jobtensor
jobtensor.com โ€บ Tutorial โ€บ PHP โ€บ en โ€บ Abstract-Classes
PHP Abstract Classes - abstract keyword, abstract functions | jobtensor
The following is an example of an implementation of the abstract class. <?php abstract class Person { public $name; // the constructor function is public by default public function __construct($name) { $this->name = $name; } abstract public function sayHello() : string; } // Child classes class Student extends Person { // inherited from parent class , needs to be defined but should follow the same parameters public function sayHello() : string { return "Hello!
๐ŸŒ
Andrew Schmelyun
aschmelyun.com โ€บ blog โ€บ the-difference-between-traits-interfaces-and-abstract-classes-in-php
The difference between Traits, Interfaces, and Abstract Classes in PHP - Andrew Schmelyun
August 7, 2022 - Good for adding structure and standardization. Want to learn more about each of these? Keep reading! An abstract class is structured a lot like a normal class, but contains abstract methods as well.
๐ŸŒ
SitePoint
sitepoint.com โ€บ php
Creating abstract properties in PHP - PHP - SitePoint Forums | Web Development & Design Community
March 15, 2009 - Hi I was just wondering if it was possible to create/emulate abstract properties in PHP. This is for a Page class, which is extended for each page in the site. A skeleton in shown below: abstract class Page { protected $title; abstract function displayContent(); protected function display(){ // display page, calling displayContent somewhere, and using the title from above // loads a page template } private function __destruct(){ $this->display()...
๐ŸŒ
PHP Tutorial
phptutorial.net โ€บ home โ€บ php oop โ€บ php abstract class
PHP Abstract Class
April 7, 2025 - }Code language: PHP (php) An abstract class can have properties and methods as a regular class.
๐ŸŒ
FlatCoding
flatcoding.com โ€บ home โ€บ abstract class in php: how it works & examples
Abstract Class in PHP: How It Works & Examples - FlatCoding
April 15, 2025 - Abstract classes and interfaces set rules for other classes, but they work in different ways. Can have abstract and concrete methods. Can have properties. Use extends (single inheritance). Methods can be public, protected, or private. Best for shared behavior and structure. Only method signatures (before PHP 8.0).