Home  >  Article  >  Backend Development  >  A brief analysis of the implementation scheme of polymorphism in PHP 5.0_PHP Tutorial

A brief analysis of the implementation scheme of polymorphism in PHP 5.0_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:38:20684browse

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

Support for late binding has been implemented in the latest release version 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 used 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, then this article may not be suitable for you, because this part of polymorphism is special: 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 PHP 5 Advantages and Disadvantages of Object Programming.

 2. What is polymorphism?

The definition of polymorphism from dictionary.com is "occurring in different forms, stages, or types in independent organizations or the same organization without fundamental difference." By By this definition, we can think of polymorphism as 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. Application of polymorphic design

We will continue to use the example of the Person base class, and now let us 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.

<?php
 $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 between different Person class objects to 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.

<?php
 $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 a lack of 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.

Although my example is not perfect, it already demonstrates the basic usage of polymorphism from the perspective of the calling code. Now we need to analyze the internal implementation of these classes. One of the great things about deriving from a base class is that the derived class can access the behavior of the parent class. This is often the default implementation, but may also occur in class inheritance chains to create more complex behavior. Below is a simple demonstration of this situation.

<?php
class Person{
 function AddFeedback($comment, $sender, $date){
 //Add feedback to the database
}
}
class David extends Person{
function AddFeedback($comment, $sender){
 parent::AddFeedback($comment, $sender,
Date('Y-m-d'));
}
}
?>
Here, the AddFeedback method implementation in the David class first calls Person ::AddFeedback method. You may notice that it mimics method overloading in C++, Java or C#. Keep in mind that this is just a simplified example, and the actual code you write is completely dependent on your actual project.
4. PHP Late binding in 5

In my opinion, late binding is what makes Java and C# so compelling. They allow base class methods to call methods using "this" or $this (even if they do not exist in the base class or calling a method in the base class may be replaced by another version in the inherited class). You can consider the following implementations to be allowed in PHP:

<?php
class Person{
 function AddFeedback($messageArray) {
 $this->ParseFeedback($messageArray);
 //Write to database
 }
}
class David extends Person{
 function ParseFeedback($messageArray){
 // Do some analysis
}
}
?>
Remember, there is no ParseFeedback in the Person class. Now, assuming you have this part of the implementation code (for the sake of this example), this will cause $myPerson to be a David object:

<?php
$myPerson = Person::GetPerson($name);
$myPerson->AddFeedback($messageArray);
?>
 A parsing error occurred! The general error message is that the method ParseFeedback does not exist or some similar information. About PHP Let’s discuss these late bindings in 5! Next, let’s summarize the concept of late binding.

Late binding means that the method call is not bound to the target object until the last moment. This means that when the method is called at runtime, those objects already have a concrete type. In our example above, you called David::AddFeedback(), and since $this in David::AddFeedback() refers to a David object, you can logically assume that the ParseFeedback() method exists - but In fact it doesn't exist because AddFeedback() is defined in Person and ParseFeedback() is called from the Person class.
Unfortunately, there is no easy way to eliminate PHP This behavior in 5. This means that when you want to create a flexible polymorphic class hierarchy you may be a little helpless.

I must point out that I choose PHP 5 is used as the expression language of this article simply because: this language does not realize the perfect abstraction of the object concept! Because PHP 5 is still in its beta phase, so this is understandable. In addition, now that abstract classes and interfaces are added to the language, late binding should also be implemented.

 5. Summary

At this point, you should have a basic understanding of what polymorphism is and why PHP 5 is not perfect at implementing polymorphism. In general, you should know how to use a polymorphic object model to encapsulate conditional behavior. Of course, this increases the flexibility of your objects and means less code to implement. Additionally, you increase the clarity of your code by encapsulating behavior that satisfies certain conditions (depending on the state of the object).

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/735109.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