Questions And Answers

More Tutorials

PHP Abstract Classes

An abstract class is a class that cannot be instantiated. Abstract classes can define abstract methods, which are methods without any body, only a definition:

abstract class MyAbstractClass {
abstract public function doSomething($a, $b);

Abstract classes should be extended by a child class which can then provide the implementation of these abstract methods. The main purpose of a class like this is to provide a kind of template that allows children classes to inherit from, "forcing" a structure to adhere to. Lets elaborate on this with an example:
In this example we will be implementing a Worker interface. First we define the interface:
interface Worker {
 public function run();

To ease the development of further Worker implementations, we will create an abstract worker class that already provides the run() method from the interface, but specifies some abstract methods that need to be filled in by any child class:
abstract class AbstractWorker implements Worker {
 protected $pdo;
 protected $logger;
 public function __construct(PDO $pdo, Logger $logger) {
 $this->pdo = $pdo;
 $this->logger = $logger;
 public function run() {
 try {
 $this->logger->log("Preparing main");
 $this->logger->log("Executing main");
 } catch (Throwable $e) {
 // Catch and rethrow all errors so they can be logged by the worker
 $this->logger->log("Worker failed with exception: {$e->getMessage()}");
 throw $e;
 private function setMemoryLimit($memoryLimit) {
 ini_set('memory_limit', $memoryLimit);
 $this->logger->log("Set memory limit to $memoryLimit");
 abstract protected function getMemoryLimit();
 abstract protected function prepareMain();
 abstract protected function main();

First of all, we have provided an abstract method getMemoryLimit(). Any class extending from AbstractWorker needs to provide this method and return its memory limit. The AbstractWorker then sets the memory limit and logs it. Secondly the AbstractWorker calls the prepareMain() and main() methods, after logging that they have been called.
Finally, all of these method calls have been grouped in a try-catch block. So if any of the abstract methods defined by the child class throws an exception, we will catch that exception, log it and rethrow it. This prevents all child classes from having to implement this themselves.
Now lets define a child class that extends from the AbstractWorker:
class TranscactionProcessorWorker extends AbstractWorker {
 private $transactions;
 protected function getMemoryLimit() {
 return "512M";
 protected function prepareMain() {
 $stmt = $this->pdo->query("SELECT * FROM transactions WHERE processed = 0 LIMIT 500");
 $this->transactions = $stmt->fetchAll();
 protected function main() {
 foreach ($this->transactions as $transaction) {
 // Could throw some PDO or MYSQL exception, but that is handled by the AbstractWorker
 $stmt = $this->pdo->query("UPDATE transactions SET processed = 1 WHERE id =
{$transaction['id']} LIMIT 1");

As you can see, the TransactionProcessorWorker was rather easy to implement, as we only had to specify the memory limit and worry about the actual actions that it needed to perform. No error handling is needed in the TransactionProcessorWorker because that is handled in the AbsractWorker.


In this page (written and validated by ) you learned about PHP Abstract Classes . What's Next? If you are interested in completing PHP tutorial, your next topic will be learning about: PHP Interfaces.

Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue:

Share On:

Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using, you agree to have read and accepted our terms of use, cookies and privacy policy.