VOOZH about

URL: https://www.geeksforgeeks.org/php/abstract-classes-in-php/

⇱ Abstract Classes in PHP - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Abstract Classes in PHP

Last Updated : 15 Mar, 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 the abstract methods declared in the parent class. An abstract class can contain abstract as well as non-abstract methods.


Output
Derived class

Important Facts About Abstract Classes in PHP

1. Cannot Create Instances of an Abstract Class

Like Java, an instance of an abstract class cannot be created in PHP.


Output
Derived class

2. Abstract Classes Can Have Constructors

Like in C++ and Java, abstract classes in PHP can have constructors.php


Output
Derived class constructor
Derived class printdata function

3. Abstract Methods Cannot Have a Body in PHP

Unlike Java, PHP does not allow an abstract method to have a body.


Output
Fatal error: Abstract function Base::printdata() cannot contain body in /home/guest/sandbox/Solution.php on line 3

Corrected Example

Comment