Home >Backend Development >PHP Tutorial >A brief analysis of the implementation scheme of polymorphism in PHP5.0_PHP Tutorial

A brief analysis of the implementation scheme of polymorphism in PHP5.0_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 16:58:34933browse

Abstract: This article will discuss the concept of polymorphism and its application in object-oriented design. It will also analyze how to use polymorphism in PHP 5 and its advantages and disadvantages.

Support for late binding has been implemented in the latest releases of PHP. Of course, there are still many problems when using its late binding function. If you are using an older version of PHP (my server is running PHP 5.0.1), 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.

1. PHP 5 and polymorphism

This article would like to discuss one of the most important parts of object-oriented programming - the design of polymorphism. To illustrate the problem, I'm using PHP 5. Before you continue reading, please make it clear that this article is not entirely about PHP. Although the language has made great strides in rapid development over the past two major versions, its object support still has some time to go before it can rival more mature languages ​​like C or Java. course.

If you are a beginner in object-oriented programming, this article may not be suitable for you, because this part of polymorphism is extraordinary: once you understand it, you will never forget it. If you want to learn a little bit about object programming and design, and you don't quite know what it means when someone says "an object is polymorphic", then this article is for you.
By 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.

2. What is polymorphism?

The definition of polymorphism from dictionary.com is "occurring in different forms, stages, or types in independent organizations or in the same organization without fundamental difference." From this definition, we can think that polymorphism Sexuality is a programming way of describing the same object through multiple states or phases. In fact, its real meaning is that in actual development, we only need to focus on the programming of an interface or base class, and do not have to worry about the specific class (class) to which an object belongs.

If you are familiar with design patterns, even if you have just a preliminary understanding, then you will understand this concept. In fact, polymorphism may be the greatest tool in pattern-based design programming. It allows us to organize similar objects in a logical way so that we don't have to worry about the specific type of the object when coding; moreover, 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 us consider a class called Person. We can subclass Person with classes called David, Charles and Alejandro. Person has an abstract method AcceptFeedback(), and all subclasses must implement this method. This means that any code that uses a subclass of the base Person class can call the AcceptFeedback() method. You don't have to check whether the object is a David or an Alejandro, just knowing that it is a Person is enough. As a result, your code only needs to focus on the "lowest common denominator" - the Person class.

The Person class in this example could also be created as an interface. Of course, there are some differences compared with the above, mainly: an interface does not give any behavior, but only determines a set of rules. A Person interface requires "You must support the AddFeedback() method", while a Person class can provide some default code for the AddFeedback() method - your understanding of this can be "If you do not 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 outline a desired set of functionality that your class implements.

3. Apply polymorphic design

We will continue to use the Person base class example, and now let's analyze a non-polymorphic implementation. The following examples use different types of Person objects - a very unsatisfactory way of programming. Note that the actual Person class is omitted. So far, we have only been concerned with the issue of code calls.

​$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!');
}
?>

This example shows objects with different behaviors, and a switch statement to distinguish different Person class objects so that they can perform their respective correct operations. Note that the feedback comments here are different for different conditions. This may not be the case in real application development; I simply illustrate the differences that exist in class implementations.

The following example uses polymorphism.

​$name = $_SESSION['name'];
​$myPerson = Person::GetPerson($name);
​$myPerson->AddFeedback('Great Article!', 'SomeReader', date('Y-m-d'));
?>

Note that there is no switch statement here, and most importantly, there is no information about what type of object Person::GetPerson() will return. And the other Person::AddFeedback() is a polymorphic method. Behavior is completely encapsulated by concrete classes. Remember, whether we are using David, Charles, or Alejandro here, the calling code never has to know what the concrete class does, only the base class.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631392.htmlTechArticleAbstract: This article will discuss the concept of polymorphism and its application in object-oriented design, and also analyze how Using polymorphism in PHP 5 and its pros and cons. In the latest released version of PHP...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn