Abstract Classes in PHP

Abstract Classes in PHP were introduced in version 5. Classes defined as abstract may not be instantiated, and any class that contains at least one abstract method must also be abstract. Methods defined as abstract simply declare the method’s signature – they cannot define the implementation.


How to create an abstract method?

We use the keyword abstract before the method name when we want to create an abstract method. If a class has an abstract method then we also add the abstract keyword before the class.

In the following example we have an abstract method foo() and so we have added abstract keyword to the class.

abstract class Sample {
	
	//abstract method
	public abstract function foo();

}

We can’t instantiate an abstract class

Since an abstract class is just a template without completely implemented methods hence it is not allowed to create its object.

//abstract class
abstract class Sample {
	
}

//this will give error - can't create object of an abstract class
$obj = new Sample();

An abstract class can have fully implemented methods

It is allowed to have fully implemented methods inside an abstract class. In the following example we have an abstract class Sample with an abstract method foo() and fully implemented method hello().

//abstract class
abstract class Sample {
	
	//abstract method
	public abstract function foo();

	//method
	public function hello() {
		printf("Hello World!");
	}

}

Inherit an abstract class

When an abstract class is inherited by the child class, the abstract method of the parent class must be implemented in the child class.

If the child class is not implementing an abstract method of the parent class then the child class must be declared abstract.


Detailed Example on Abstract Classes

When designing class hierarchies, you might want to partially leave certain methods for inheriting classes to implement. For example, say you have the class hierarchy shown in Figure.

It might make sense to implement setCenter($x, $y) in class Shape and leave the implementation of the draw() methods to the concrete classes Square and Circle. You would have to declare the draw() method as an abstract method so that PHP knows you are intentionally not implementing it in class Shape.

The class Shape would then be called an abstract class, meaning that it’s not a class with complete functionality and is only meant to be inherited from.

Note:You cannot instantiate an abstract class. You can define any number of methods as abstract, but once at least one method of a class is defined as abstract, the entire class needs to be declared as abstract, too.

This double definition exists to give you the option to define a class abstract even if it doesn’t have
any abstract methods, and to force you to define a class with abstract methods as abstract so that it is clear to others what you had in mind. The previous class diagram would translate into PHP code that’s similar to the following:

abstract class Shape {
 function setCenter($x, $y) {
 $this->x = $x;
 $this->y = $y;
 }
 abstract function draw();
protected $x, $y;
}
class Square extends Shape {
 function draw()
 {
 // Here goes the code which draws the Square
 ...
 }
}
class Circle extends Shape {
 function draw()
 {
 // Here goes the code which draws the Circle
 ...
 }
}

This is all about abstract classes in PHP.

Leave a Comment

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