


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");
}
? >
Run result:
Area: 35
Sides: 4
Area: 10
Sides: 3
Area: 28.274333882308
Color:blue
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.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Alipay PHP...

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

An official introduction to the non-blocking feature of ReactPHP in-depth interpretation of ReactPHP's non-blocking feature has aroused many developers' questions: "ReactPHPisnon-blockingbydefault...
