This article mainly shares with you the usage of abstract classes and interfaces in PHP. I hope it can help you. Let's first share with you the concept of abstract methods.
1. Abstract method
In a class, a method without a method body is an abstract method.
abstract Visibility function method name (parameter 1,...); // If visibility is not specified explicitly, the default is public
For example:
public function hello($args); abstract function work(); // 修饰符abstract,也可以省略
2. Abstract class
abstract class class name{
Attribute;
Method ;
##Abstract classes cannot be instantiated and can only be inherited.
Abstract classes do not necessarily have abstract methods. A class with abstract methods must be an abstract class.
The visibility of abstract methods cannot be private
Abstract methods are in subclasses , needs to be rewritten.
When do you need to use abstract classes?
#When it is necessary to control a class that can only be inherited and cannot be instantiated.
Example: Declare a human with an abstract method that works.
abstract class People{ protected $age=22; public $height=1.70; abstract function work(); } class PhpTeacher extends People{ function work(){ echo "真不是php"; } }
3. Interface
If all methods in a class are abstract methods and have no member attributes, then this class is It's called an interface.
interface Common{ abstract function work(); abstract function test($args); }
The role of interface: Although PHP classes have single inheritance, multiple inheritance can be achieved through interfaces.
Interface inheritance (extends):
Interface inheritance interface interface interface name extends parent interface name
Note: Class inheritance is single Inheritance (there can only be one parent class), but the inheritance of the interface is multiple inheritance, and the implementation of the interface by the class is also multiple implementations.
Interface implementation (implements):
Class implements the interface class Class name implements Interface name 1, Interface name 2, ...
Inherit the class and implement the interface at the same time:
The class inherits the parent class and implements the interface at the same time class class name extends parent class name implements interface name
4. The difference between abstract classes and interfaces
The interface is a special abstract class that only contains Abstract method, no member properties.
When a class implements an interface (implements), it must fully implement all methods in the interface; when a class inherits (extends) an abstract class, Just rewrite the abstract methods you need.
Abstract classes can only have single inheritance, but interfaces have multiple inheritance, and the class's implementation of the interface also has multiple implementations.
##Related recommendations:
php abstract Detailed explanation of classes
Detailed explanation of abstract classes and interfaces in PHP
Abstract classes and interfaces in PHP
The above is the detailed content of Usage of abstract classes and interfaces in PHP. For more information, please follow other related articles on the PHP Chinese website!