PHP and MySQL (56 Blogs) Become a Certified Professional

How To Implement Abstract Class In php?

Published on Aug 13,2019 1.6K Views

How To Implement Abstract Class In php?

If we want to write a certain class method, but we are only sure about the name of the method, and not the details of how it should be written, we use abstract class in PHP. When we want the child classes to be committed to certain methods that they inherit from the parent class but we cannot commit about the code that should be written inside the methods.Then we use abstract classes and methods.

So let us explore above mentioned subject with following pointers,

So let us get started then,

Abstract Class in PHP

A Class that has at least one method, which is a method without any actual code in it, just the name and the parameters, and that has been marked as “abstract” is known as an abstract class. When we want to define an abstract class we need to use the keyword abstract. In order to provide a kind of template to inherit from and to force the inheriting class to implement the abstract methods, we use an abstract class. It can contain both abstract as well as non abstract methods.

Moving on with this Abstract Class In PHP,

Create an abstract class


<?php //abstract class abstract class school { // abstract function teach abstract public function teach(); } ?>

In the example above, our class school is an abstract class, which has an abstract method. If you want to create a new class which extends our class school then you will have to provide a definition for the abstract method teach, else the child class should also be abstract. It is compulsory for all the child classes to provide a definition for method teach().

Moving on with this Abstract Class In PHP

Non abstract methods inside an abstract class

Non-abstract methods can also be present in Abstract classes along with or without abstract methods. So abstract class are also said to be known as partially implemented classes. They can be accessed and used directly by the child classes, without overriding them.

<?php //abstract class abstract class school { //It is protected variable protected $subject; //It is non-abstract public function english public function english() { echo $this->subject. " English Subject
//It is non-abstract public function computer
public function computer()
echo $this->subject. " Computer Science subject
//It is non-abstract public function tenthClassa
public function tenthClass($group)		{
$this->subject = $group;
//It is abstract public function teach
abstract public function teach();

In the above, we have added three non-abstract methods english(), computer() and tenthClass() to our abstract school class.

Moving on with this Abstract Class In PHP

Working Of Abstract Class

Below example demonstrates the working of abstract class

<?php abstract class school { //abstract method only needs to define the required arguments abstract protected function subject($name); } class ConcreteClass extends school { public function subject($sub) { $sub1=$sub; return $sub; } } $obj = new ConcreteClass; echo $obj->subject("English");
echo "
echo $obj->subject("Computer Science");

feature Image - Abstract Class In PHP - Edureka

With this we come to an end of this article, I hope you understood the Abstract Class in PHP, creating an abstract class, using non abstract methods inside an abstract class. If you found this article relevant, check out the PHP Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.

Got a question for us? Please mention it in the comments section of  this article and I will get back to you.


Join the discussion

Browse Categories

webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

How To Implement Abstract Class In php?