Simply say Abstract method means We declare it in one class and use(define) it in other class.
What is Abstract in PHP?
PHP 5 supports the abstract classes and methods in OOPs concept.A class contain the abstract methods is known as an abstract class and that class specified with the keyword abstract.An abstract class can not instantiate objects means if you are trying to instantiate class with a new keyword which is not possible and class must be specified with at least one abstract method and When you inherit from an abstract class, all the abstract methods in the superclass must be defined in the sub(extended) class.
Note that an abstract class contain both the abstract methods and other methods.
ABSTRACT Method FORMAT:
1 2 3 | [abstract typename(Parameter_list)] |
Introduction:
- Abstract class was introduced in PHP 5.3 and in OOP, it’s not in PHP 4
- Prefix or Keyword: “abstract” for class and method
- Concept: we need to extend this class by its Superclass or classes.
The concept of the abstract class came from Java.Abstract class mainly create confusion for us because of Interface.The abstract class can’t be directly initiated with the new operator, we need to extend it with other class which is not abstract.
Recommended Read: Constructor and Destructor in PHP and Type Hinting in PHP5.
An abstract class isn’t a fully defined method because we need to create other class to extend it.
NOTE We can derive class from abstract class. Object of an abstract class can’t be possible.
Basic difference between Interface and Abstract class:
The interface can be implemented and Abstract class can be extended and PHP can support multiple interfaces, but it does not support multiple inheritances with class extend.It only supports multi-level inheritance so I can say if multi-level inheritance required or multiple inheritances is not required, you can prefer Abstract class.
Access modifiers(Public, Protected and Private) are not mandatory but if you are using it,make sure the method declared in subclass have same access modifier or less when creating a subclass of extending abstract class.
Let’s see example how access modifier works with Abstract
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | abstract class Shape { } Class Triangle extends Shape { protected function showarea() { $t=0.5 * $this->d1 * $this->d2; return $t; } // Common method public function display() { } } $t1 = new Triangle(); $t1->d1 = 10; $t1->d2 = 10; $t1->display(); |
Here, In above example have protected access modifier for abstract method showarea(), so in the subclass you need to use protected or public access modifier. If you try to use private, it will generate fatal error
Fatal error: Access level to Triangle::showarea() must be protected (as in class Shape) or weaker
Basic Syntax to declare Abstract Class and Abstract Method:
1 2 3 4 5 6 7 | abstract function FunctionName(){ } // Replace FunctionName with your desired function name } |
NOTE: If any class contains abstract method then the class must be declared as abstract.
Important Points:
- In Abstract class, we can declare abstract method but we can’t define it.
- The Subclasses which inherits the property of abstract class, must define all the methods declared as abstract.
- If a method is defined as abstract then it cannot be declared as private (it can only be public or protected) otherwise its can not be inherited.
- Method declared on the subclass must have a same or lower access level that the abstract method declared in the super class. So if super class have protected abstract method then we can have that method either public or protected but we can not declare it as a private.
Let’s move to one Example so you get straight-knowledge of it.
Let’s create one PHP application which declare abstract class Shape with two members say d1 and d2.This class declare abstract method show area which inherit the class triangle from Shape class.You can create a class with the keyword abstract even if it does not have a single abstract method.
We can say Shape is our Superclass and Triangle is the subclass. Subclass Triangle defines Showarea method declare as abstract in shape class.We create one object of Triangle class and will display it and make it sure that Object of abstract Shape class can’t be created.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | abstract class Shape { abstract function showarea(); } Class Triangle extends Shape { function showarea() { $t=0.5 * $this->d1 * $this->d2; echo $t; } } $t1 = new Triangle(); $t1->d1 = 10; $t1->d2 = 10; $t1->showarea(); |
Here, New operator or I can say Object used to call Triangle class which extend our Abstract class and Triangle class has one method showarea() so it will call abstract class Shape because method is declared in abstract class so indirectly new operator will call the Shape Class.
NOTE: New operator indirectly call the super class or Abstract class by using a subclass.
If you need help for developing code using abstract, feel free to ask me via the comment section below.
Comments (2)