Php object-oriented -- polymorphism _ PHP Tutorial

Source: Internet
Author: User
Php object-oriented-polymorphism. Php object-oriented -- polymorphism first explains polymorphism: php object-oriented polymorphism refers to the method implemented by defining subclasses through an abstract class or interface, php object-oriented in a function or class-polymorphism
First, let's explain polymorphism: php object-oriented polymorphism refers to the method implemented by defining subclasses through an abstract class or interface, then, input the names of objects after each subclass instantiation defined previously in a function or class method, and call the same method in the new class (because the parameters are different) different execution logics of different objects are implemented. (Your understanding is not official ).

The following is an example:

// Define the parent class interface

Interface Person {

Function getName ();

Function getAge ();

}

// The first subclass

Class Phper implements Person {

Public function getName ()

{

Echo 'this is php \ 'getname ';

}

Public function getAge ()

{

Echo 'this is php \ 'getage ';

}

}

// The second subclass

Class Javaer implements Person {

Public function getName ()

{

Echo 'this is jave \ 'getname ';

}

Public function getAge ()

{

Echo 'this is java \ 'getage ';

}

}

// A brand new class

Class my {

Function isMy ($ obj) // The parameter here is an object name

{

$ Obj-> getName ();

Echo"
";

$ Obj-> getAge ();

}

}

// Several classes before instantiation

$ Php = new Phper;

$ Java = new Javaer;

$ My = new My;

$ My-> isMy ($ java); // execute different business logic by passing in different object names

?>

If you want to ask me how to use this item, I can only say that it may be helpful in terms of code specifications and understanding. I am not sure about the others. this is because I have never encountered application polymorphism in my actual work.

I hope Boyou can give explanations or links in comments for new insights or better cases.

Explain the polymorphism first: php object-oriented polymorphism refers to defining the method implemented by the subclass through an abstract class or interface, and then defining the method in a function or class...

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.