What are abstract methods and abstract classes in php

王林
Release: 2023-03-03 13:34:01
Original
2077 people have browsed it

Abstract method refers to a method defined in a class without a method body. If a method in a class is an abstract method, then the class is an abstract class, and abstract classes can contain non-abstract methods. When declaring abstract methods and abstract classes, you need to use the abstract keyword to modify them.

What are abstract methods and abstract classes in php

#What is an abstract method?

(Recommended tutorial: php tutorial)

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();
Copy after login

What is an abstract class?

As long as a method in a class is an abstract method, then the class is defined as an abstract class, and the abstract class must also be modified with the "abstract" keyword; in an abstract class, there can be methods that are not abstract and Member attributes, but as long as one method is abstract, the class must be declared as an abstract class and decorated with "abstract".

For example:

abstract class demo{
var $test;
abstract function fun1();
abstract function fun2();
}
Copy after login

The above is the detailed content of What are abstract methods and abstract classes in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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