Home > Article > Backend Development > Abstraction: Understand the abstract classes in PHP through specific programs
Of course, there may be multiple root classes to implement different functions. In a well-designed system, each root class should have a useful interface that can be used by application code. If our application code is designed If it works with the root class, it can also work with any subclass that inherits from the root class.
Abstract methods are placeholders like general methods in subclasses (taking up space but not working), It is different from a normal method - there is no code. If one or more abstract methods exist in a class, then the class becomes an abstract class. You cannot instantiate abstract classes. You must inherit them and then instantiate subclasses. You also An abstract class can be thought of as a template for a subclass.
If you override all abstract methods, the subclass becomes an ordinary class. If you do not override all methods, the subclass is still abstract. If a class If it contains an abstract method (even if there is only one), you must declare that the class is abstract and add abstract before the class keyword.
The syntax for declaring abstract methods is different from declaring general methods. Abstract methods are not as common as general methods. The main part contained in curly brackets {} and ended with a semicolon;
In the following program file, we define a class Shape that contains the getArea method. However, it is impossible to determine the area of the figure because the shape is not known. Indeed we declared the getArea method as abstract. You cannot instantiate a Shape object, but you can inherit from it or use it in an expression, as in Example 6.13.
If you create a class with only abstract methods , you define an interface. To illustrate this situation, there are interface and implements keywords in PHP. You can use interface instead of abstract classes, and implements instead of extends to illustrate your class definition or use an interface. . For example, you can write a myClass implements myIterface. These two methods can be chosen according to personal preference.
Code
Copy code The code is as follows:
/*Note:
The two methods refer to:
1. abstract class aaa{} (note that there are only abstract methods in aaa, no general methods)
class bbb extends aaa{} (overwrite the abstract methods in aaa in bbb)
2. interface aaa{}
class bbb implements aaa {} (override the abstract method in aaa in bbb)
*/
//abstract root class abstract root class
abstract class Shape
{
abstract function getArea(); //Define an abstract method
}
//abstract child class abstract subclass
abstract class Polygon extends Shape //Polygon
{
abstract function getNumberOfSides();
}
//concrete class entity class triangle class
class Triangle extends Polygon
{
public $ base;
public $height;
public function getArea() //Override the area calculation method
{
return(($this->base * $this->height)/2);
}
public function getNumberOfSides () //Override edge count method
{
return(3);
}
}
//concrete class entity class quadrilateral
class Rectangle extends Polygon
{
public $width;
public $height;
public function getArea()
{
return($this->width * $this->height);
}
public function getNumberOfSides()
{
return(4);
}
}
//concrete class entity class Circle
class Circle extends Shape
{
public $radius;
public function getArea()
{
return(pi() * $this->radius * $this->radius);
}
}
/ /concrete root class defines a color class
class Color
{
public $name;
}
$myCollection = array(); //Create a collection of shapes and put it into the array
//make a rectangle
$r = new Rectangle ;
$r->width = 5;
$r->height = 7;
$myCollection[] = $r;
unset($r);
//make a triangle
$t = new Triangle;
$t->base = 4;
$t->height = 5;
$myCollection[] = $t;
unset($t);
//make a circle
$c = new Circle;
$c->radius = 3;
$myCollection[] = $c;
unset($c);
//make a color
$c = new Color;
$c->name = "blue";
$myCollection[] = $c;
unset($c);
foreach($myCollection as $s)
{
if($s instanceof Shape) print("Area: ".$s->getArea() ."
n"); //If $s is an instance of the Shape class
if($s instanceof Polygon) print("Sides: ".$s->getNumberOfSides()."
n ");
if($s instanceof Color) print("Color:".$s->name."
n");
print("
n");
}
? >
The above has introduced abstraction. Through specific programs, we can understand the abstract classes in PHP, including abstract aspects. I hope it will be helpful to friends who are interested in PHP tutorials.