Home >Backend Development >PHP Tutorial >Detailed explanation of php abstract methods and abstract class instances
This article mainly introduces PHP abstract methods and abstract classes, and analyzes the concepts, functions, definitions and usage of PHP abstract methods and abstract classes in the form of examples. Friends in need can refer to the following
for details As follows:
What is an abstract method?
A method defined in a class without a method body is an abstract method. When the method is declared, there are no curly braces and the content within it. In addition, when declaring an abstract method, keywords must be added. abstract to modify.
For example:
abstract function fun1(); abstract function fun2();
As long as there is an abstract method in a class, then this class must be defined as an abstract class.
Abstract classes should also be modified with abstract.
Abstract classes can have methods and member attributes that are not abstract.
But as long as there is an abstract method, this class must be defined as an abstract class.
How to use abstract classes? The most important point is that abstract classes cannot produce instance objects!
Defining an abstract class is equivalent to defining a specification! This specification requires subclasses to comply! After a subclass inherits an abstract class, it implements the abstract methods in the abstract class according to the needs of the subclass.
The subclass must implement all the abstract methods in the abstract class, otherwise there are still abstract methods in the subclass, the subclass is still an abstract class, and it still cannot be instantiated!
<?php abstract class Demo{ var $test; abstract function fun1(); abstract function fun2(); function fun3(){ ... } } class Test extends Demo{ function fun1(){ ... } function fun2(){ ... } } $test = new Test(); ?>
The above is the entire content of this article, I hope it will be helpful to everyone's study.
Related recommendations:
PHPRealize multi-process and multi-thread
##PHP5 ways to obtain user access IP address
The above is the detailed content of Detailed explanation of php abstract methods and abstract class instances. For more information, please follow other related articles on the PHP Chinese website!