Section 10--Abstract methods and abstract classes_PHP tutorial
/*
+-------------------------------------------------- ----------------------------------+
| = This article is read by Haohappy>
| = Notes from the Chapter Classes and Objects
| = Translation + personal experience
| = To avoid possible unnecessary trouble, please do not reprint, thank you
| = We welcome criticisms and corrections, and hope to make progress together with all PHP enthusiasts!
| = PHP5 Research Center: http://blog.csdn.net/haohappy2004
+---------- -------------------------------------------------- ------------------+
*/
Section 10--Abstract methods and abstract classes
Object-oriented programming through the hierarchical structure of classes Build it up. In single inheritance languages such as PHP, class inheritance is tree-like. A root class has one or more subclasses, and each subclass inherits one or more subclasses. Of course. , there may be multiple root classes used 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 for general methods in subclasses (take up space but do not work) , which is different from general methods - 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 You can also think of an abstract class 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 contains an abstract method (even if there is only one), you must declare the class to be abstract by adding abstract before the class keyword.
The syntax for declaring abstract methods is different from declaring general methods. The syntax of abstract methods There is no body part enclosed in curly braces {} like a normal method, and ended with a semicolon;.
In Example 6.13, we defined a class Shape that contains the getArea method. But it is impossible because we don’t know the shape. To determine the area of the shape, 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 When you create a class with only abstract methods, you define an interface. To illustrate this situation, PHP has the interface and implements keywords. You can use interface instead of abstract class and implements instead of extends to illustrate this. Your class defines or uses an interface. For example, you can write a myClass implements myIterface. These two methods can be chosen according to personal preference.
/*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 abstract methods in aaa in bbb)
*/
Listing 6.13 Abstract classes
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- & gt; base*$ this- & gt; height)/2);
}
Public function getnumberofsides () // The statistical method of overwriting edges
{
Return (3);
}
}
//concrete class Solid class Polygon
class Rectangle extends Polygon
{
public $width;
public $height; > Public function getArea()
return(4);
}
//concrete class entity class Circle
class Circle extends Shape
{
public $radius;
public function getArea()
pi() * $this->radius * $this->radius);
} }
}
//concrete root class defines a color class > 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) //If $s is an instance of Shape class
{
print("Area: ". $s->getArea().
$s->getNumberOfSides( ) . $s->name
n ");
}
print("
n");
}
?>
http://www.bkjia.com/PHPjc/316938.html
www.bkjia.com
true

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.


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

SublimeText3 Chinese version
Chinese version, very easy to use

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment