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 recent 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), then you may find that support for late binding is lacking. 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 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 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's 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 a base class is beyond the topic 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. Applying 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('Ym-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 is used to distinguish different Person class objects so as 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.
An example below uses polymorphism.
<?php
$name = $_SESSION['name'];
$myPerson = Person::GetPerson($name);
$myPerson->AddFeedback('Great Article!', 'SomeReader', date('Ym-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 demonstrates the basic use 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 database}
}
class David extends Person{
function AddFeedback($comment, $sender){
parent::AddFeedback($comment, $sender,
date('Ym-d'));
}
}
?>
Here, the Person::AddFeedback method is first called in the implementation of the AddFeedback method in the David class. 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. Late binding in PHP 5
In my opinion, late binding is an important reason why Java and C# are 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);
?>
An analysis error occurred! The general error message is that the method ParseFeedback does not exist or some similar information. Let’s talk about late binding in PHP 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 this behavior in PHP 5. This means that you may be a bit powerless when you want to create a flexible polymorphic class hierarchy.
I must point out that I chose PHP 5 as the expression language for this article simply because: this language does not realize the perfect abstraction of the object concept! This is understandable since PHP 5 is still in its beta version. 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 in achieving 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).