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 PHP5 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), 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, 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 the advantages and disadvantages of object programming in PHP 5.
2. What is polymorphism?
Polymorphism, the definition from dictionary.com is "appearing in different forms, stages or types in independent organization or the same type of organization, there is no 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 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 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. 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.
The following is a quote fragment:
<?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!'); >