Analysis of the implementation scheme of polymorphism in PHP 5.0 _php tutorial

Source: Internet
Author: User
Summary: This article will discuss the concept of polymorphism and its application in object-oriented design, and will also analyze how to use polymorphism in PHP5 and the advantages and disadvantages of its existence.

Support for late binding has been implemented in the latest releases of PHP. Of course, there are many problems when using its late binding feature. If you are using an older version of PHP (my server is running PHP 5.0.1), then you may find that there is a lack of support for late binding. Therefore, please note that the code in this article may not work in your specific version of PHP 5.

  One, PHP 5 and polymorphism

This article wants to discuss the design of polymorphism, one of the most important parts of object-oriented programming. To illustrate the problem, I used PHP 5. Before you continue reading, make sure that this article is not entirely about PHP. Although this language has made great strides in rapid development in the previous two major releases, it has a history of support for objects before it rivals more mature languages such as C + + or java.

If you are an introduction to object-oriented programming, this article may not be suitable for you because polymorphism is a special part of the knowledge: once you understand it, you will never forget it. This article is right for you if you want to learn a little bit about object programming and design knowledge, and when someone says "something is polymorphic" and is not quite sure what it means.

At the end of this article, you should know what polymorphism is and how to apply it to object-oriented design, and you will understand the advantages and disadvantages of object programming in PHP 5.

  Second, what is polymorphism?

Polymorphism, which comes from the definition of dictionary.com, is "in different forms, stages or types that appear in an independent organization or in the same organization, without the fundamental difference." "By this definition, we can think of polymorphism as a way of describing the same object through a variety of states or stages." In fact, the real meaning of it is: in real development, we only need to focus on the programming of an interface or a base class, without worrying about the specific class that an object belongs to.

If you are familiar with design patterns, even if there is a preliminary understanding, then you will understand this concept. In fact, polymorphism may be the greatest tool in programming based on pattern design. It allows us to organize similar objects in a logical way so that we do not have to worry about the exact type of the object in the specific encoding, and we only need to program a desired interface or base class. The more abstract an application is, the more flexible it becomes-and polymorphism is one of the best ways to abstract behavior.

For example, let's consider a class called person. We can subclass a person with classes called David,charles and Alejandro. The person has an abstract method Acceptfeedback (), and all subclasses implement this method. This means that any code that uses a subclass of the base class person can call Method Acceptfeedback (). You don't have to check whether the object is a David or a Alejandro, just know that it is a person enough. As a result, your code only needs to focus on the "lowest common denominator"-person class.

The person class in this example can also be created as an interface. Of course, there are some differences compared to the above: one interface does not give any behavior, and only a set of rules is determined. A person interface requires "You must support the Addfeedback () method", and a person class can provide some default code for the Addfeedback () method-you can understand it as "if you don't choose to support Addfeedback (), Then you should provide a default implementation. "How to choose an interface or base class is not the subject of this article;" However, in general, you need to implement a default method through the base class. You can also use an interface if you can simply sketch out a set of desired functions that your class will implement.

  Third, the application of polymorphism design

We will continue to use the example of the person base class, and now let us analyze a non-polymorphic implementation. Different types of person objects are used in the following examples--a very undesirable way to programmatically do this. Note that the actual person class is omitted. So far, we are only concerned with the issue of code invocation.

The following is a reference fragment:

$name = $_session[' name '];

$myPerson = Person::getperson ($name);

Switch (Get_class ($myPerson)) {

Case ' David ':

$myPerson->addfeedback (' Great article! ', ' Some Reader ', date (' y-m-d '));

Break

Case ' Charles ':

$myPerson->feedback[] = Array (' Some Reader ', ' great editing! ');

Break

Case ' Alejandro ':

$myPerson->feedback->append (' Awesome javascript! ');

Break

Default:

$myPerson->addfeedback (' yay! ');

}

? >


http://www.bkjia.com/PHPjc/446954.html www.bkjia.com true http://www.bkjia.com/PHPjc/446954.html techarticle Summary: This article will discuss the concept of polymorphism and its application in object-oriented design, and will also analyze how to use polymorphism in PHP5 and the advantages and disadvantages of its existence. The latest release of PHP ...

  • Related Article

    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.