Abstract Classes in PHP | Object Oriented Concepts

A Child Class can inherit properties from a Parent Class. Also, you can directly create an object of a Parent Class. However, you can’t instantiate an object of an Abstract Class directly. In this article, we will discuss Abstract Classes in PHP. Also, we will discuss their implementation along with a few examples.

You might want to read about Inheritance in PHP before reading this article if you are not very familiar with it.

What are Abstract Methods?

A method which only gives its signature and not the implementation is an Abstract Method. It depends on the Child Class to define an implementation of an Abstract Method. They are declared by prefixing the abstract keyword.

In short, an abstract method is simply a function definition that serves to tell the programmer that the method must be implemented in a child class.

What are Abstract Classes?

An Abstract Class is a class with at least one Abstract Method. Any class inheriting an Abstract Class need to provide an implementation of all it’s Abstract Methods. Additionally, these methods should be defined with the same visibility. For example, if the abstract method is defined as protected, the function implementation must be defined as either protected or public, but not private. Also, the signature of these methods must match (number of parameters). However, any default parameter in an Abstract Method may not exist in the inherited Class Method.

Note: An abstract class can contain abstract as well as non-abstract methods. It can’t be instantiated directly.

// Abstract classes are declared with the abstract keyword, and contain abstract methods.
abstract class BaseClass {
  abstract public function baseMethod();
}
PHP Abstract Classes Diagram

Inheriting an Abstract Class

Just like inheriting a regular class, you can also inherit an Abstract Class using the extends keyword. The only difference is that it is mandatory to implement the abstract methods from the parent abstract class.

<?php

	// Abstract classes are declared with the abstract keyword, and contain abstract methods.
	abstract class BaseClass {
  		abstract public function baseMethod();

  		protected function regularMethod() {
  			echo 'Base Regular Method';
  		}
	}

	class ChildClass extends BaseClass {
		public function baseMethod() {
			echo 'ChildClass';
		}
	}

	class SecondChildClass extends BaseClass {
		public function baseMethod() {
			echo 'Second Child Class';
		}
	}

	$childObj = new ChildClass();
	$childObj->baseMethod();
	/* ChildClass */

	$secondChild = new SecondChildClass();
	$secondChild->baseMethod();
	/* Second Child Class */

?>

You can observe the following in the above code snippet:

  • An Abstract Class in PHP is a class with at least one Abstract Method.
  • It can contain abstract as well as non-abstract methods.
  • Unlike non-abstract methods, it is mandatory to override abstract methods.
  • The Abstract Methods may have different definitions in different child classes.

Common Mistakes While Using Abstract Classes

Here are some common misconceptions and mistakes while using Abstract Classes in PHP.

Instantiating an Object of Abstract Class

<?php
	// Abstract classes are declared with the abstract keyword, and contain abstract methods.
	abstract class BaseClass {
  		abstract public function baseMethod();

  		protected function regularMethod() {
  			echo 'Base Regular Method';
  		}
	}

	$baseClass = new BaseClass();
	//Fatal error: Uncaught Error: Cannot instantiate abstract class BaseClass
?>

You cannot instantiate an object of an Abstract Class.

Defining the implementation of an Abstract Method

<?php
	abstract class BaseClass {
  		abstract public function baseMethod() {
   			echo 'Base Method';
  		}
       }
?>

This results in the following error: Abstract Method BaseClass::baseMethod cannot contain the body.

Incorrectly implementing Abstract Methods

It is mandatory to implement abstract methods with the same visibility and signatures. Failure to do either of them results in an error.

Incorrect Visibility

<?php
	// Abstract classes are declared with the abstract keyword, and contain abstract methods.
	abstract class BaseClass {
  		abstract public function baseMethod();
	}

	class ChildClass extends BaseClass {
		private function baseMethod() {
			echo 'ChildClass';
		}
	}
?>

Access Level to ChildClass::baseMethod must be public.

The Child Class must always implement an Abstract Method with the same visibility as in the Parent Class.

Incorrect Signature

<?php
	// Abstract classes are declared with the abstract keyword, and contain abstract methods.
	abstract class BaseClass {
  		abstract public function baseMethod($value1);
	}

	class ChildClass extends BaseClass {
		public function baseMethod() {
			echo 'ChildClass';
		}
	}
?>

Declaration of ChildClass::baseMethod must be compatible with BaseClass::baseMethod($value1).

The Child Class must include all the mandatory parameters in the definition of Abstract Method. However, it may not implement optional parameters.

Conclusion

In conclusion, we discussed Abstract Classes in PHP. They serve to give an outline to a programmer for Child Classes. You cannot directly instantiate its object. You can learn more about Object Oriented Concepts in PHP on Concatly. Further, you can read more about them on the Official Documentation of PHP.

Do like and share the article with your friends. Kindly leave your valuable comments to help us improve this resource.

Spread the Knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *