Examples of object programming polymorphism in PHP

Source: Internet
Author: User
This article provides an example to illustrate the polymorphism of object programming in PHP. it is the basic knowledge in PHP beginners. For more information, see What is polymorphism?
Polymorphism, defined by dictionary.com, is "in different forms, stages or types appear in an independent organization or in the same type of organization, without any fundamental difference. "From this definition, we can think that polymorphism is a programming method that describes the same object through multiple states or stages. In fact, its true significance lies in: in actual development, we only need to pay attention to the programming of an interface or base class, rather than worrying about the specific class of an object ).
If you are familiar with the design model, you will understand this concept even if you only have a preliminary understanding. In fact, PHP5 polymorphism may be the greatest tool in pattern-based programming. It allows us to organize similar objects in a logical way so that we don't have to worry about the specific types of objects during specific encoding. Moreover, we only need to program a desired interface or base class. The more abstract an application is, the more flexible it is. polymorphism is one of the best ways to abstract behavior.
For example, let's consider a class called Person. We can use classes called David, Charles, and Alejandro to subclass Person. Person has an abstract method AcceptFeedback (), which must be implemented by all subclasses. This means that any code that uses the subclass of the base class Person can call the AcceptFeedback () method (). You don't have to check whether the object is a David or an Alejandro. you only need to know if it is a Person. The result is that your code only needs to focus on the "minimum public denominator"-Person class.
In this example, the Person class can also be created as an interface. Of course, there are some differences with the above, mainly because an interface does not give any behavior, but only determines a group of rules. A Person interface requires "you must support the AddFeedback () method", while a Person class can provide some AddFeedback () default method code-you can understand it as "If you do not choose to support AddFeedback (), you should provide a default implementation. "As for how to select an interface or base class, it is not the topic of this article. however, generally, you need to implement a default method through the base class. If you can briefly outline a set of desired functions to be implemented by your class, you can also use an interface.

General understanding
The most direct definition of polymorphism is to allow different class objects with inheritance relationships to call member functions with the same name to produce different response results.

Multi-State Code

<? Php/*** declare interface Demo * @ author wzy **/interface Demo {const NAME = "wangzhengyi"; const AGE = 25; function fun1 (); // The default declaration method is public abstract function fun2 ();} class One implements Demo {public function fun1 () {echo Demo: NAME. "studied at media University of China";} public function fun2 () {echo Demo: NAME. "The age is ". demo: AGE ;}} class Two implements Demo {public function fun1 () {echo Demo: NAME. "Beijing lingchuangzhong and technology Limited company internship ";} public function fun2 () {echo Demo: NAME. "Last year's age was 24" ;}/// the same interface implements the same method, different objects, and different response results. This is the manifestation and application of polymorphism $ one = new One (); $ one-> fun1 (); // wangzhengyi studied at $ one-> fun2 (); // The Age of wangzhengyi is 25 echo"
"; $ Two = new Two (); $ two-> fun1 (); // wangzhengyi $ two-> fun2 () in Beijing lingchuangzhong and Technology Co., Ltd (); // wangzhengyi was 24 years old last year

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.