Home  >  Article  >  Backend Development  >  What are abstract methods and abstract classes in php

What are abstract methods and abstract classes in php

王林
王林Original
2020-07-29 14:25:432044browse

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();

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();
}

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!

Statement:
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