The concepts and differences between abstract classes and interfaces in php. This article provides a detailed analysis of the concepts and differences between abstract classes and interfaces in php. For more information, see copy the code below: definition of abstract classes: abstract This article provides a detailed analysis of the concepts and differences between abstract classes and interfaces in php. For more information, see
The code is as follows:
// Definition of abstract classes:
Abstract class ku {// define an abstract class
Abstract function kx ();
......
}
Function aa extends ku {
// Method for implementing the abstract class
Function kx (){
Echo 'sdsf ';
}
}
// Usage
$ Aa = new aa;
$ Aa-> kx ();
// 1. define some methods. the subclass must fully implement all methods in this abstract.
// 2. an object cannot be created from an abstract class. Its significance lies in being extended.
// 3. abstract classes usually have abstract methods without braces
// 4. abstract methods do not need to implement specific functions, which are completed by sub-classes.
// 5. when a subclass implements an abstract class method, its subclass visibility must be greater than or equal to the definition of an abstract method.
// 6. the abstract class method can have parameters or be empty.
// 7. If the abstract method has parameters, the subclass must have the same number of parameters.
//// // Definition of the interface class:
Interface Shop {
Public function buy ($ gid );
Public function compute ($ gid );
Abstract function view ($ gid );
}
// If you want to use an interface, you must define one of the methods in the interface class, except abstract ).
// In this way, if you do the following methods in a large project, you must implement all the methods in this interface!
// Example: a method to implement the above interface
Class BaseShop implements Shop {
Public function buy ($ gid ){
Echo 'The item with ID: '. $ gid.' You purchased ';
}
Public function compute ($ gid ){
Echo 'items with the ID you purchased: '. $ gid ';
}
Public function view ($ gid ){
Echo 'The item with ID: '. $ gid.' You browsed ';
}
}
// Multiple inheritance examples of interfaces:
Interface staff_i1 {// interface 1
Function setID ();
Function getID ();
}
Interface staff_i2 {// interface 2
Function setName ();
Function getName ();
}
Class staff implements staff_i1, staff_i2 {
Private $ id;
Private $ name;
Function setID ($ id ){
$ This-> id = $ id;
}
Function getID (){
Return $ this-> id;
}
Function setName ($ name ){
$ This-> name = $ name;
}
Function getName (){
Return $ this-> name;
}
Function otherFunc () {// This is a method that does not exist in an interface
Echo "Test ";
}
}
?>
Their differences:
1. Abstract classes can have non-abstract methods, while interfaces can only have abstract methods!
2. A class can inherit multiple interfaces, while a class can only inherit one abstract class!
3. The interface is used by the implements keyword, while the abstract class is inherited by the extends keyword!
The alias code is as follows: // The definition of the abstract class: abstract...