Abstract Class in PHP with Example

Mohasin Hossain
3 min readSep 14, 2023

Today we going to explore what is Abstract class in PHP and how to use it.

Few things we need to know about abstract class.

  1. An Abstract class provides a template — or base — for any sub classes which extends the Abstract class.
  2. An Abstract class is never be instantiate.
  3. Abstract method has no code but signature & there is no implementation, that’s be main difference between Abstract & Concreate.
  4. Abstract method is a requirement or must implement for sub class which extends the Abstract class.
  5. Abstraction is a model of a real-world object or phenomenon, limited to a specific context which represents all details relevant to this context with high accuracy and omits all the rest.

Now let play with some simple code.

abstract class Animal {
protected $name;

public function __construct($name) {
$this->name = $name;
}

// Abstract method (no implementation)
abstract public function makeSound();

// Concrete method (has implementation)
public function getName() {
return $this->name;
}
}

class Dog extends Animal {
public function makeSound() {
return "Woof!";
}
}

class Cat extends Animal {
public function makeSound() {
return "Meow!";
}
}

// You cannot create an instance of the abstract class Animal
// $animal = new Animal("Generic Animal"); // This will result in an error

// Create instances of the concrete classes Dog and Cat
$dog = new Dog("Rex");
$cat = new Cat("Whiskers");

// Call methods on instances
echo $dog->getName() . " says " . $dog->makeSound() . "\n"; // Output: Rex says Woof!
echo $cat->getName() . " says " . $cat->makeSound() . "\n"; // Output: Whiskers says Meow!

So, you see if any class like Dog, Cat extends Animal class must implement makeSound (abstract method).

If any subclass will not implement abstract method then will get an error.

Also you see we can not create an instance of the abstract class Animal, if we do so then got error again.

Now let’s see some real world code example

// Abstract Report class
abstract class Report {
protected $data;

public function __construct($data) {
$this->data = $data;
}

// Abstract method for generating the report
abstract public function generateReport();

// Concrete method for saving the report to a file
public function saveReport($fileName) {
$reportContent = $this->generateReport();
file_put_contents($fileName, $reportContent);
echo "Report saved to $fileName\n";
}
}

// Concrete PDFReport class
class PDFReport extends Report {
public function generateReport() {
// Generate PDF report from $this->data and return it as a string
$report = "PDF Report\n";
$report .= "Data: " . implode(', ', $this->data);
return $report;
}
}

// Concrete CSVReport class
class CSVReport extends Report {
public function generateReport() {
// Generate CSV report from $this->data and return it as a string
$report = "CSV Report\n";
$report .= implode(',', $this->data);
return $report;
}
}

// Example usage
$data = [1, 2, 3, 4, 5];

$pdfReport = new PDFReport($data);
$pdfReport->saveReport('pdf_report.pdf');

$csvReport = new CSVReport($data);
$csvReport->saveReport('csv_report.csv');

In summary, abstract classes provide a way to define common behavior and structure while allowing for customization in subclasses. They are a powerful tool for creating well-organized, reusable, and extensible object-oriented code. When you have a group of classes that share some common features but also have differences, abstract classes provide a structured way to model that relationship.

For more details of OOP (Object Ordinated Programming) , you may have a look at the link given below. Feel free to leave a comment if you have any feedback or questions.

https://github.com/mohasin-dev/Object-Oriented-Programming-in-PHP

Bonus: You must love my most read article : How To Use Single Responsibility Principle in PHP/Laravel
And most GitHub star repo:
https://github.com/mohasinhossain/SOLID-Principles-in-PHP

--

--

Mohasin Hossain

Senior Software Engineer | Mentor @ADPList | Backend focused | PHP, JavaScript, Laravel, Vue.js, Nuxt.js, MySQL, TDD, CI/CD, Docker, Linux