Introduction to the PHP abstract class

  • 2020-05-17 04:57:45
  • OfStack

In natural language, we understand the abstract concept of a large description of an object that is a property common to a class of objects. So in PHP, it's also like 1, we abstract a class, we can specify the behavior of class 1, this class should be a template, it indicates the behavior of its submethods must be implemented.
Definition of PHP abstract class application:
abstract class ClassName{
}

Application points of PHP abstract class:
1. Define methods that subclass must fully implement all methods in the abstraction
2. You cannot create an object from an abstract class; it is meant to be extended
3. Abstract classes usually have abstract methods without curly braces

Key application points of PHP abstract class:
1. Abstract methods do not have to implement specific functions; they are done by subclasses
2. When a subclass implements an abstract class method, the visibility of the subclass must be greater than or equal to the definition of the abstract method
Abstract class methods can have arguments or be empty
4. If the abstract method has parameters, the subclass implementation must have the same number of parameters

Example application of PHP abstract class:
abstract public function_name (); // notice no curly braces
PHP abstract class rules:
A class must be declared an abstract class as long as it has at least one abstract method
An abstract method that cannot contain the body of a function
A subclass of an abstract class that implements an abstract method must have the same or lower level of access as the abstract method
A subclass that inherits an abstract class and is an abstract class if it does not implement all abstract methods
As a demonstration, let's implement a simple abstract class: calculate the area of a rectangle. This rectangle can be extended from the shape class.
 
< ?PHP 
abstract class Shape { 
abstract protected function get_area(); 
// and 1 The difference is that this method does not have curly braces  
// You cannot create an instance of this abstract class: $Shape_Rect= new Shape(); 
} 
class Rectangle extends Shape{ 
private $width; 
private $height; 
function __construct($width=0, 
$height=0){ 
  $this->width=$width; 
  $this->height=$height; 
} 
function get_area(){ 
  echo ($this->width+$this->height)*2; 
} 
} 
$Shape_Rect = new Rectangle(20,30); 
$Shape_Rect->get_area(); 
?> 

This is also a simple example, which basically explains the use of abstract classes in PHP. I don't want to talk about the rest. I think abstract class 1 will be used in big projects, because I think it is too much "rules" to follow, it is not convenient to use! That's just my opinion, of course. Also want to say more about 1, PHP abstract class application is single inheritance, that is to say you can only inherit from 1 class, and not 1 class inherited from A class, and then inherit from B class, if you want to achieve such a function, you have to use the interface related knowledge, here we will not discuss PHP interface knowledge! Single inheritance in multiple interfaces!

Related articles: