PHP object-oriented development

Source: Internet
Author: User
Introduction and advantages of class polymorphism 1 polymorphism. 2 operator: instanceof. 3. simple application of polymorphism. 1 Introduction and advantages of polymorphism: Polymorphism inherits the abstract and inheritance of object-oriented language

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.

The format code is as follows:

  1. Class {}
  2. Class B {}
  3. $ Thing = new;
  4. If ($ thing instanceof ){
  5. Echo "";
  6. }
  7. If ($ thing instanceof B ){
  8. Echo "B ";
  9. }

3. simple application of polymorphism

The code for instance 1 is as follows:

  1. Class {
  2. }
  3. Class B {
  4. }
  5. $ New = new;
  6. If ($ new instanceof ){
  7. Echo "";
  8. }
  9. If ($ new instanceof B ){
  10. Echo "B ";
  11. }
  12. ?>

The code for instance 2 is as follows:

  1. Interface MyUsb {
  2. Function type ();
  3. Function alert ();
  4. }
  5. Class Zip implements MyUsb {
  6. Function type (){
  7. Echo & quot; 2.0 & quot ";
  8. }
  9. Function alert (){
  10. Echo "the USB flash drive is detecting ...... ";
  11. }
  12. }
  13. Class Mp3 implements MyUsb {
  14. Function type (){
  15. Echo & quot; 1.0 & quot ";
  16. }
  17. Function alert (){
  18. Echo "the MP3 driver is detecting ...... ";
  19. }
  20. }
  21. Class MyPc {
  22. Function Add_Usb ($ what ){
  23. $ What-> type ();
  24. $ What-> alert ();
  25. }
  26. }
  27. $ P = new MyPc ();
  28. $ Zip = new Zip ();
  29. $ Mp3 = new Mp3 ();
  30. $ P-> Add_Usb ($ zip );
  31. $ P-> Add_Usb ($ mp3 );
  32. ?>

Add an instance code:

  1.  
  2.  
  3. Inheritance and polymorphism 
  4.  
  5.  
  6. /* Parent class */
  7. Class MyObject {
  8. Public $ object_name; // book name
  9. Public $ object_price; // book price
  10. Public $ object_num; // number of books
  11. Public $ object_agio; // book discount
  12. Function _ construct ($ name, $ price, $ num, $ agio) {// Constructor
  13. $ This-> object_name = $ name;
  14. $ This-> object_price = $ price;
  15. $ This-> object_num = $ num;
  16. $ This-> object_agio = $ agio;
  17. }
  18. Function showMe () {// output function
  19. Echo 'is not displayed. ';
  20. }
  21. }
  22. /* Subclass Book */
  23. Class Book extends MyObject {// subclass of MyObject.
  24. Public $ book_type; // type
  25. Function _ construct ($ type, $ num) {// declare the constructor
  26. $ This-> book_type = $ type;
  27. $ This-> object_num = $ num;
  28. }
  29. Function showMe () {// override the showMe method in the parent class
  30. Return 'This New'. $ this-> book_type. 'book'. $ this-> object_num .'
    ';
  31. }
  32. }
  33. /* Subclass Elec */
  34. Class Elec extends MyObject {// another subclass of MyObject
  35. Function showMe () {// override the showMe method in the parent class
  36. Return 'hot books: '. $ this-> object_name .'
    Original price: '. $ this-> object_price .'
    Special offer: '. $ this-> object_price * $ this-> object_agio;
  37. }
  38. }
  39. /* Instantiate the object */
  40. $ C_book = new Book ('computer class', 1000); // declare a Book subclass object
  41. $ H_elec = new Elec ('php function reference ', 0.8,); // declare an Elec subclass object
  42. Echo $ c_book-> showMe ()."
    "; // Output the showMe () method of the Book subclass.
  43. Echo $ h_elec-> showMe (); // The showMe () method is used to output the Elec subclass.
  44. ?>
  45.  
  46.  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.