Home > Backend Development > PHP Tutorial > Detailed explanation of php abstract methods and abstract class usage examples

Detailed explanation of php abstract methods and abstract class usage examples

伊谢尔伦
Release: 2023-03-12 12:20:01
Original
1616 people have browsed it

In OOP language, a class can have one or more subclasses, and each class has at least one public method as an interface for external code to access it. The abstract method is introduced to facilitate inheritance. Let's first take a look at the definition of abstract class and abstract method before explaining its use.

Abstract methods and abstract classes
In OOP language, a class can have one or more subclasses, and each class has at least one public method as
The interface through which external code accesses it. Abstract methods are introduced to facilitate inheritance. Let's first take a look at the definitions of abstract classes and
abstract methods before explaining their uses.
What is an abstract method? The method we define in the class without a method body is an abstract method. The so-called method body means that when the method is declared, there are no curly brackets and its contents, but directly in the method name when declaring it. After
, add a semicolon to end. In addition, when declaring an abstract method, add a keyword "abstract" to modify it;
For example:
abstract function fun1();
abstract function fun2() ;
The above example is the abstract method "fun1()" and "fun2()" without a method body modified by "abstract". Don't forget that
there is a semicolon after the abstract method; so what is abstraction? What about classes? As long as there is a method in a class that is an abstract method, then the class must be defined as an abstract class, and the abstract class must also be modified with the "abstract" keyword; in an abstract class, the surface can be abstract or not. Methods and members
attributes
, but as long as one method is an abstract method, this class must be declared
as an abstract class and decorated with "abstract". For example: Code snippet

abstract class Demo{ 
var $test; 
abstract function fun1(); 
abstract function fun2(); 
function fun3(){ 
… . 
} 
}
Copy after login

In the above example, an abstract class "Demo" is defined and modified with "abstract", and a
member attribute is defined in this class" $test", two abstract methods "fun1" and "fun2" and a non-abstract method fun3(); then

how do we use the abstract class? The most important point is that abstract classes cannot produce instances

objects
, so they cannot be used directly. We have mentioned many times that classes cannot be used directly. What we use is instantiated by classes. Object, then an abstract class cannot produce instance objects. What is the use of declaring an abstract class? We use abstract methods as templates for subclasses
overloading. Defining an abstract class is equivalent to defining a specification. This specification requires subclasses to comply. Subclasses Following the abstract class, implement the abstract methods in the abstract class according to the needs of the subclass. The subclass must implement all the abstract methods in the parent class. Otherwise, if there are still abstract methods in the subclass, the subclass will still be an abstract class and cannot be instantiated. Why do we have to start from the abstract class? What about inheritance? Because sometimes if we want to implement some functions, we must inherit from the abstract class, otherwise
you will not be able to implement these functions. If you inherit the abstract class, you must implement the abstract method in the class;
Code snippet



The above is the detailed content of Detailed explanation of php abstract methods and abstract class usage examples. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template