Free cookie consent management tool by TermsFeed Generator PHP Abstract Class | Amir Kamizi
AMIR KAMIZI
Home Blog Courses Books Newsletter Store Membership Buy me a coffee
PHP Abstract Class

PHP Abstract Class

Last Updated on Mar 22, 2023

Introcution

In order to understand abstract classes you should know 4 topics:

  1. class
  2. inheritance
  3. interface
  4. access modifiers

We have already talked about those topics in other posts so by know you should know them.

What is an abstract class

Well simply put, an abstract class is like an interface so it has methods that are only defined and not implemented but at the same time it’s like a class and can have methods that are implemented.

It’s like having the best of both worlds

Let’s see an example

<?php
abstract class Human
{
    private $name;
    
    public function __construct(string $name)
    {
        $this->name = $name;
    }
    
    public function sayHello()
    {
        echo 'Hello my name is '.$this->name;
    }
    
    abstract protected function nationality();
}

abstract class vs class

As you can see the difference with a class is that it has the word abstract in the name. Abstract class. And for the methods that are going to be defined only and not implemented we should use the same keyword. Abstract public/protected function.

Let’s see an example of a class that extends this abstract class and implement it.

class Pratham extends Human {
    
    public function nationality()
    {
        echo "I am from India";
    }
}

$pratham = new Pratham('Pratham');
$pratham->sayHello();
// Hello my name is Pratham
$pratham->nationality();
// I am from India

access modifiers in an abstract class

Do you see how the abstract class is protected but when we implement it, we made it public?

If the abstract method is protected the implemented method can be protected or public but if the abstract method is public then the implemented method must be public as well.

abstract class vs interface

Let’s compare our abstract class with an interface.

There are 4 main differences.

  1. abstract classes can have properties and as you have seen we had name in the abstract class. We can’t have properties in an interface.
  2. As you have seen, we had an abstract method that was protected. In an interface all the methods must be public.
  3. you cannot implement a method inside an interface but as you can see we have implemented the sayHello method in our abstract class.
  4. if your class wants to implement the methods of an interface you should use implements but for abstract class we use extends

let's take a look at it again

<?php
abstract class Human
{
    private $name;
    
    public function __construct(string $name)
    {
        $this->name = $name;
    }
    
    public function sayHello()
    {
        echo 'Hello my name is '.$this->name;
    }
    
    abstract protected function nationality();
}

class Pratham extends Human {
    
    public function nationality()
    {
        echo "I am from India";
    }
}

$pratham = new Pratham('Pratham');
$pratham->sayHello();
// Hello my name is Pratham
$pratham->nationality();
// I am from India

https://youtu.be/mHcalz7oU84

Conclusion

Now you know about abstract classes in PHP.

I recommend you to open a PHP files and try writing an abstract class. add abstract methods to it and try to extend it and implement its abstract methods.

If you have any suggestions, questions, or opinions, please contact me. I’m looking forward to hearing from you!

Key takeaways

  • define an abstract class
  • abstract class vs class
  • abstract class vs interface
  • abstract methods
  • access modifiers in an abstract class

Category: programming

Tags: #php #oop

Join the Newsletter

Subscribe to get my latest content by email.

I won't send you spam. Unsubscribe at any time.

Related Posts

Top Open Source Licenses: Understanding Your Options
May 07, 2024 programming

Top Open Source Licenses: Understanding Your Options

Open source licenses dictate how software can be used, modified, and distributed. Understanding the different types of licenses is essential for developer. ...

9 Min Read Read More
How to Split an Excel File into Multiple Files Using Python
May 08, 2024 programming

How to Split an Excel File into Multiple Files Using Python

Sometimes you might have an excel files that you need to split. splitting the file into multiple files with Python can be easily done. ...

7 Min Read Read More
New PHP Tools You’ve Probably Never Heard Of
Jul 15, 2025 programming

New PHP Tools You’ve Probably Never Heard Of

The PHP ecosystem is constantly evolving, with fresh packages, libraries, and tools emerging that aim to solve old problems in new ways. There’s a growing landscape of lesser-known tools quietly gaining traction within the PHP community. ...

10 Min Read Read More
Laravel 12 Beginner to Advanced: Complete Guide
Jul 17, 2025 programming

Laravel 12 Beginner to Advanced: Complete Guide

Laravel is a name that resonates throughout the PHP community. With its elegant syntax, robust ecosystem, and developer-friendly tools, Laravel has become the go-to framework for modern web development. Laravel 12, released on February 24, 2025, continues this tradition, streamlining workflows and boosting performance with support for the latest PHP advancements. ...

20 Min Read Read More

Recommended Courses

Introduction to Machine Learning in PHP

Introduction to Machine Learning in PHP

Learn to Build Different Machine Learning Models Easily ...

PHP Tutorial Beginner to Advanced

PHP Tutorial Beginner to Advanced

Learn everything you need to start a successful career as a PHP developer ...