Questions about polymorphism in PHP

Source: Internet
Author: User
Tags class manager
The question about polymorphism in PHP Wikipedia has a definition about polymorphism: in object-oriented languages, interfaces are implemented in a variety of different ways: 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 public function paintbrush () of the painter class {// defines the painter action echo "the painter is painting! \ N ";}} class typist {// defines the typist class public function typed () {// defines the typist job echo" the typist is typing! \ N ";}} function printworking ($ obj) {// defines the processing class if ($ obj instanceof painter) {// if the object is a painter class, the painter action $ obj-> paintbrush ();} elseif ($ obj instanceof typist) is displayed {// if the object is a typist class, the typist action $ obj-> typed ();} else {// If not, the Error message echo "Error: object Error! ";}} Printworking (new painter (); // display the employee's work printworking (new typist (); // display the employee's work



To demonstrate polymorphism, the following code segment uses polymorphism:
Class employee {// defines the employee's parent class protected function working () {// defines the employee's work. echo must be implemented in the subclass. "This method needs to be overloaded in the subclass! ";}} Class painter extends employee {// defines the painter class public function working () {// implements the inherited work method echo" the painter is painting! \ N ";}} class typist extends employee {// defines the typist class public function working () {echo" the typist is typing! \ N ";}} class manager extends employee {// defines the manager class public function working () {echo" the manager is in a meeting! ";}} Function printworking ($ obj) {// define the processing method if ($ obj instanceof employee) {// if it is an employee object, the working status $ obj-> working ();} else {// otherwise, the Error message echo "Error: object Error! ";}} Printworking (new painter (); // display the painter's work printworking (new typist (); // display the typist's work printworking (new manager ()); // display the manager's work


The next code unifies the method names of different sub-classes to achieve polymorphism. Is this a correct example of polymorphism? If yes, can it be understood that all subclasses inheriting an abstract class are polymorphism compared to the abstract parent class?


In another forum discussion post (http://bbs.csdn.net/topics/390025104), teacher xuzuning of 55L gave a sample code:
Class Animal {var $ voice = 'undefined'; var $ root; function _ construct () {$ this-> root = $ this;} function signal ($ o) {$ this-> root = $ o;} function Talk () {echo $ this-> root-> voice ;}} class Cat extends Animal {var $ voice = 'meak';} class Dog extends Animal {var $ voice = 'wangwang ';} $ a = new Animal; $ a-> Talk (); $ a-> signal (new Cat); $ a-> Talk (); $ a-> signal (new Dog ); $ a-> Talk ();


In this code, the variable $ a first declares the Animal parent class as an example. Therefore, the output is "undefined" and then declares it as a subclass Cat and Dog, when Cat is declared, the variable $ a points to the Cat instance, so calling $ a-> talk () is actually calling Cat's talk. Similarly, when $ a declares a Dog instance, it overwrites the cat object. In the post, instructor xuzuning said that the parent object can operate in different ways based on the features assigned to its sub-objects, however, this code does not show that $ a calls the talk method through the parent class step. Instead, the new object overwrites the previous object and calls the new object method completely. Is this code example of xuzuning inappropriate?


Recently, I have been puzzled by the specific practice of polymorphism. some articles mention the need for forced type definition, and some use the instanceof keyword in the sample code to determine whether to inherit from a class. Which teacher can give a clear definition and code example and answer whether the above code example is a multi-state practice?


Reply to discussion (solution)

The example works. The first is more typical. it is best to use a virtual function in the parent class to ensure subclass implementation.

The key to polymorphism is "the same interface" and "different implementations". do not tangle with other details ....

The example works. The first is more typical. it is best to use a virtual function in the parent class to ensure subclass implementation.

The key to polymorphism is "the same interface" and "different implementations". do not tangle with other details ....

Thank you, teacher helloyou0. Are there any virtual functions in PHP?

Php should say abstract functions...
Http://www.php.net/manual/en/language.oop5.abstract.php

Savvy savvy ....

This is a bit like a Buddhist scriptures. you can understand the concept and use abstract methods to describe specific abstract behaviors.

However, this code does not show that $ a calls the talk method through the parent class step. Instead, the new object overwrites the previous object and calls the new object method completely. You know who you are in print_r ($ ).

Php should say abstract functions...
Http://www.php.net/manual/en/language.oop5.abstract.php

Some people say that the virtual function is static delayed binding. I think it is more like. Why can abstract functions ensure the implementation of sub-classes?

Reference the reply from chenlong451: but this code does not show that $ a calls the talk method through the parent class, but the new object overwrites the previous object, the calling method is completely the method of the new object. You know who you are in print_r ($ ).

Thank you very much for your prompt.

The result after print_r is

Animal Object ([voice] => undefined [root] => Dog Object ([voice] => Wang [root] => Dog Object * RECURSION *))

It turns into an array of objects and retains the voice static attribute in the parent class. What's going on? Why not overwrite it?

Why is it covered?
Tell me why

Why is it covered?
Tell me why

Read the code carefully. Shame.

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.