Questions about polymorphism in PHP

Source: Internet
Author: User
The question about polymorphism in PHP This post was last edited by chenlong451 in Wikipedia from 2013-03-2823: 51: 16. there is a definition about polymorphism: in object-oriented language, many different implementation methods of the interface are Polymorphism. please refer to this article: Questions about polymorphism in http://www.cnblogs.com/tecs27/archive/2012/03 PHP
At the end of this post, the definition of polymorphism in Wikipedia was edited by chenlong451 at 23:51:16. in object-oriented languages, the implementation of interfaces is polymorphism.

Read this article:
Http://www.cnblogs.com/tecs27/archive/2012/03/13/2394028.html

Two sample codes are provided in this article. the code segment that uses flow control is as follows:

Class painter {// defines the painter class
Public function paintbrush () {// defines the painter action
Echo "the painter is painting! \ N ";
}
}

Class typist {// defines the typist class
Public function typed () {// defines the work of a typist
Echo "Typist Typing! \ N ";
}
}

Function printworking ($ obj) {// defines the processing class
If ($ obj instanceof painter) {// if the object is a painter class, the painter action is displayed.
$ Obj-> paintbrush ();
} Elseif ($ obj instanceof typist) {// if the object is a typist class, the typist action is displayed.
$ Obj-> typed ();
} Else {// if the preceding class is not used, the error message is displayed.
Echo "Error: object Error! ";
}
}

Printworking (new painter (); // Display employees' work
Printworking (new typist (); // Display employees' work



To demonstrate polymorphism, the following code segment uses polymorphism:

Class employee {// defines the employee parent class
Protected function working () {// defines employee work, which must be implemented in the subclass
Echo "this method needs to be reloaded in the subclass! ";
}

}

Class painter extends employee {// defines the painter class
Public function working () {// how to implement inheritance
Echo "the painter is painting! \ N ";
}
}

Class typist extends employee {// defines the typist class
Public function working (){
Echo "Typist Typing! \ N ";

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.