PHP object-oriented development and other polymorphism details. This article describes the polymorphism of PHP object-oriented development. I hope this tutorial will be helpful to you. Class polymorphism 1. introduction and advantages of polymorphism. 2. operators this article will introduce you to a detailed explanation of polymorphism such as PHP object-oriented development. I hope this tutorial will be helpful to you.
Class polymorphism
1. introduction and advantages of polymorphism.
2. OPERATOR: instanceof.
3. simple application of polymorphism.
1. introduction and advantages of polymorphism
Introduction: polymorphism is the third feature of object-oriented language after being abstracted and inherited.
For example, the USB interface uses different functions to insert different things.
Advantage: OOP not only combines many functions and functions, but describes a situation in our life using classes, inheritance, and polymorphism.
2. OPERATOR: instanceof
PHP is a type operator used to determine whether a given object comes from a specified object.
Format:
| The code is as follows: |
|
|
Class {}
Class B {}
$ Thing = new;
If ($ thing instanceof ){
Echo "";
}
If ($ thing instanceof B ){
Echo "B ";
} |
3. simple application of polymorphism
Instance 1:
| The code is as follows: |
|
|
Class {
}
Class B {
}
$ New = new;
If ($ new instanceof ){
Echo "";
}
If ($ new instanceof B ){
Echo "B ";
}
?> |
Instance 2:
| The code is as follows: |
|
|
Interface MyUsb {
Function type ();
Function alert ();
}
Class Zip implements MyUsb {
Function type (){
Echo & quot; 2.0 & quot ";
}
Function alert (){
Echo "the USB flash drive is detecting ......
";
}
}
Class Mp3 implements MyUsb {
Function type (){
Echo & quot; 1.0 & quot ";
}
Function alert (){
Echo "the MP3 driver is detecting ...... ";
}
}
Class MyPc {
Function Add_Usb ($ what ){
$ What-> type ();
$ What-> alert ();
}
}
$ P = new MyPc ();
$ Zip = new Zip ();
$ Mp3 = new Mp3 ();
$ P-> Add_Usb ($ zip );
$ P-> Add_Usb ($ mp3 );
?> |
Add an instance 213.29.11.16 update
Inheritance and polymorphism
/* Parent class */
Class MyObject {
Public $ object_name; // book name
Public $ object_price; // book price
Public $ object_num; // number of books
Public $ object_agio; // book discount
Function _ construct ($ name, $ price, $ num, $ agio) {// Constructor
$ This-> object_name = $ name;
$ This-> object_price = $ price;
$ This-> object_num = $ num;
$ This-> object_agio = $ agio;
}
Function showMe () {// output function
Echo 'is not displayed. ';
}
}
/* Subclass Book */
Class Book extends MyObject {// subclass of MyObject.
Public $ book_type; // type
Function _ construct ($ type, $ num) {// declare the constructor
$ This-> book_type = $ type;
$ This-> object_num = $ num;
}
Function showMe () {// override the showMe method in the parent class
Return 'This New'. $ this-> book_type. 'book'. $ this-> object_num .'
';
}
}
/* Subclass Elec */
Class Elec extends MyObject {// another subclass of MyObject
Function showMe () {// override the showMe method in the parent class
Return 'hot books: '. $ this-> object_name .'
Original price: '. $ this-> object_price .'
Special offer: '. $ this-> object_price * $ this-> object_agio;
}
}
/* Instantiate the object */
$ C_book = new Book ('computer class', 1000); // declare a Book subclass object
$ H_elec = new Elec ('php function reference ', 0.8,); // declare an Elec subclass object
Echo $ c_book-> showMe ()."
"; // Output the showMe () method of the Book subclass.
Echo $ h_elec-> showMe (); // The showMe () method is used to output the Elec subclass.
?>
Bytes. Class polymorphism 1. introduction and advantages of polymorphism. 2. operators...