Home >Backend Development >PHP Tutorial >Analysis of the concepts and usage of abstract classes and abstract methods in PHP, php abstraction_PHP tutorial
The examples in this article describe abstract classes and abstract methods in PHP. Share it with everyone for your reference, the details are as follows:
1. Abstract keyword: abstract
Abstract means that it cannot be explained exactly, but it has a certain concept or name. To declare an abstract class or method in PHP, we need to use the abstract keyword.
2. Definition of abstract methods and abstract classes
At least one method in a class is abstract, we call it an abstract class. So if you define an abstract class, first define the abstract method.
abstract class class1{ abstract function fun1(); …… }
1. There is at least one abstract method in the class
2. Abstract methods are not allowed to have { }
3. Abstract methods must be preceded by abstract
3. Rules for using abstract classes and methods
Several characteristics of abstract classes:
1. cannot be instantiated and can only be inherited
2. In the inherited derived class, all abstract methods must be overloaded before they can be instantiated
Example:
<?php abstract class cl1{ abstract function fun1(); abstract function fun2(); } class cl2 extends cl1{ function fun1(){ echo "第一个"; } function fun2(){ echo "第二个"; } } $c=new cl2(); echo $c->fun2(); ?>
Readers who are interested in more PHP related content can check out the special topics of this site: "Summary of PHP File Operations", "Summary of PHP Operations and Operator Usage", "Summary of PHP Network Programming Skills", "Introduction Tutorial on PHP Basic Grammar" ", "Summary of PHP office document operation skills (including word, excel, access, ppt)", "Summary of PHP date and time usage", "Introduction to PHP object-oriented programming tutorial", "Summary of PHP string (string) usage" , "Introduction Tutorial on PHP MySQL Database Operation" and "Summary of Common PHP Database Operation Skills"
I hope this article will be helpful to everyone in PHP programming.