As a new version, PHP5 supports late binding. However, in actual applications, the late binding feature still has some problems. For example, you are using an older version of PHP, then you may find that there is no support for late binding. What we are talking about today is aboutI. PHP 5 and Polymorphism
This article will 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 to read this article, make it clear that this article is not entirely about PHP. Although this language has made great progress in rapid development in the previous two major versions, before it matches more mature languages such as C ++ or Java, its support for objects also goes through a process.
If you are an object-oriented programmer, this article may not be suitable for you, Because PHP5 polymorphism is a special part of the knowledge: Once you understand it, you will never forget it. If you want to learn a little bit about object programming and design, and it is not clear what it means when someone says "an object is a polymorphism", this article is for you.
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.
2. What is polymorphism?
Polymorphism, defined by dictionary.com, is "in different forms, stages or types appear in an independent organization or in the same type of organization, without any 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 true significance lies in: in actual development, we only need to pay attention to the programming of an interface or base class, rather than worrying about the specific class of an object ).
If you are familiar with the design model, you will understand this concept even if you only have a preliminary understanding. In fact, PHP5 polymorphism may be the greatest tool in pattern-based programming. It allows us to organize similar objects in a logical way so that we don't have to worry about the specific types of objects during specific encoding. Moreover, we only need to program a desired interface or base class. The more abstract an application is, the more flexible it is. polymorphism is one of the best ways to abstract behavior.
For example, let's consider a class called Person. We can use classes called David, Charles, and Alejandro to subclass Person. Person has an abstract method AcceptFeedback (), which must be implemented by all subclasses. This means that any code that uses the subclass of the base class Person can call the AcceptFeedback () method (). You don't have to check whether the object is a David or an Alejandro. You only need to know if it is a Person. The result is that your code only needs to focus on the "minimum public Denominator"-Person class.
In this example, the Person class can also be created as an interface. Of course, there are some differences with the above, mainly because an interface does not give any behavior, but only determines a group of rules. A Person interface requires "you must support the AddFeedback () method", while a Person class can provide some AddFeedback () default method code-you can understand it as "if you do not choose to support AddFeedback (), you should provide a default implementation. "As for how to select an interface or base class, it is not the topic of this Article. However, generally, you need to implement a default method through the base class. If you can briefly outline a set of desired functions to be implemented by your class, you can also use an interface.
Iii. Application of PHP5 polymorphism Design
We will continue to use the Person base class example. Now let's analyze a non-polymorphism implementation. Different types of Person objects are used in the following example-this is a very bad programming method. Note that the actual Person class is omitted. So far, we only care about 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 the objects with different behaviors, and a switch statement is used to distinguish different Person class objects and execute their corresponding correct operations. Note: feedback comments for different conditions are different here. This may not happen in actual application development; I just want to briefly describe the differences in class implementation.
The following example uses PHP5 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 the most important thing is that there is a lack of objects of the type returned by Person: GetPerson. Another Person: AddFeedback () is a polymorphism method. The behavior is completely encapsulated by a specific class. Remember, whether we are using David, Charles or Alejandro here, it is okay to call the code without having to know the functions of the specific class, but only the basic class.
Although my example is not perfect, it shows the basic usage of PHP5 polymorphism from the perspective of calling code. Now we need to analyze the internal implementation of these classes. One of the greatest ways to derive from a base class is that the derived class can access the behavior of the parent class, which is often the default implementation, however, it may also appear in the class inheritance chain for creating more complex behaviors. The following is a simple example 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 "Person: AddFeedback" method is called in the AddFeedback method implementation of the David class. You may have noticed that it imitates method overloading in C ++, Java, or C. Remember, this is just a simple example, and the actual code of your PHP5 polymorphism is fully dependent on your actual project.