1. What is an abstract method?
The method we define in the class without a method body is an abstract method. The so-called no method body means that there are no curly braces and the contents inside when declaring, but directly adding a semicolon after the method name when declaring. In addition, when declaring an abstract method, the method also needs to add a keyword. "abstract" to modify.
For example:
abstract function fun1(); abstract function fun2();
2. What is an abstract class?
As long as there is a method in a class that is an abstract method, then the class is defined as an abstract class. The abstract class must also be modified with the "abstract" keyword; in the abstract class, there can be whether it is abstract or not. methods and member attributes, but as long as one method is abstract, the class must be declared as an abstract class and decorated with "abstract".
Related recommendations: "PHP Tutorial"
For example:
abstract class Demo { var $test; abstract function fun1(); abstract function fun2(); function fun3(){ ... } }
3. What is the role of abstract classes?
In the above example, an abstract class "demo" is defined and modified with "abstract". In this class, a member attribute "$test" and two abstract methods "fun1" are defined. There is also a non-abstract method fun3() with "fun2";
So how do we use abstract classes? The most important point is that abstract classes cannot produce instance objects, so they cannot be used directly. We have mentioned many times before that classes cannot be used directly. We use objects instantiated through classes.
So abstract classes cannot produce instance objects, what is the use of declaring abstract classes? We use abstract methods as templates for subclass overloading. Defining an abstract class is equivalent to defining a specification. This specification requires subclasses to comply. After the subclass succeeds the abstract class, the abstract class in the abstract class Methods are implemented as needed by subclasses. The subclass must implement all abstract methods in the parent class. Otherwise, if there are still abstract methods in the subclass, then the subclass will still be an abstract class and cannot instantiate objects;
Why do we have to start from What about inheritance in abstract classes? Because sometimes if we want to implement some functions, we must inherit from an abstract class, otherwise you will not be able to implement these functions. If you inherit an abstract class, you must implement the abstract methods in the class;
For example :
<? abstract class Demo { var $test; abstract function fun1(); abstract function fun2(); function fun3() { ... } } $demo=new Demo(); //抽象类不能产生实例对象,所以这样做是错的,实例化对象交给子类 class Test extends Demo { function fun1() { ... } function fun2() { ... } } $test = new Test(); //子类可以实例化对象,因为实现了父类中所有抽象方法 ?>
4. Characteristics of abstract classes:
Abstract classes cannot be instantiated and can only be inherited.
Abstract classes do not necessarily have abstract methods. A class with abstract methods must be an abstract class.
The visibility of abstract methods cannot be private.
Abstract methods are in subclasses and need to be rewritten.
5. When do you need to use abstract classes?
There is a method, but I don’t know how to write the method body. When this method must be included in the subclass, it is encapsulated into an abstract method and the class is an abstract class.
When certain methods must be encapsulated in the control subclass, abstract methods can be used.
When it is necessary to control a class that can only be inherited and cannot be instantiated.
6. Interface
If all methods in a class are abstract methods and have no member attributes, the class is called an interface.
interface Common{ abstract function work(); abstract function test($args); }
Interface inheritance (extends):
Interface inheritance interface interface interface name extends parent interface name
Note: Class inheritance is single inheritance (there can only be one parent class), but the inheritance of the interface is multiple inheritance, and the implementation of the interface by the class is also multiple implementation.
Interface implementation (implements):
Class implements the interface class class name implements interface name 1, interface name 2, ...
Inherits the class and implements the interface at the same time:
Class inherits the parent class and implements the interface at the same time class class name extends parent class name implements interface name
7. The difference between abstract class and interface
Interface is A special abstract class whose interface only contains abstract methods and no member attributes. When a class implements an interface, it must fully implement all methods in the interface;
When a class inherits (extends) an abstract class, it only needs to rewrite the abstract methods that need to be used. Abstract classes can only have single inheritance, but interfaces have multiple inheritance, and the class's implementation of the interface also has multiple implementations.
The above is the detailed content of What is the role of php abstract class. For more information, please follow other related articles on the PHP Chinese website!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver CS6
Visual web development tools