Application Example of Polymorphism in PHP Object Oriented Programming

  • 2021-11-13 00:58:00
  • OfStack

This paper describes the application of polymorphism in PHP object-oriented programming with examples. Share it for your reference, as follows:

Polymorphism is another important feature among the three characteristics of object-oriented, besides encapsulation and inheritance. It shows the function of dynamic binding, also known as "heteromorphism with the same name". Polymorphic functions can make the software reach sufficient extensibility during development and maintenance. In fact, the most direct definition of polymorphism is that different class objects with inheritance relationship can call member functions with the same name and have different reaction effects. The so-called polymorphism refers to the ability of a program to deal with multiple types of objects. In PHP, polymorphic values refer to the rewriting of methods. Method override means that some methods in the parent class can be modified in a subclass to make them have their own characteristics. Overrides require that the methods of the child class have the same name as the methods of the parent class, which can be regulated by declaring abstract classes or interfaces.

We through the computer USB device application to introduce 1 under the object-oriented polymorphism, the current USB settings only our own used I think there are more than 10. For example, USB mouse, USB keyboard, USB storage devices, etc. The external devices of these computers are connected to the computer through USB interface, and then called and started by the computer. That is to say, while the computer is running normally, it expands one function for the computer without inserting one different USB device, which is what we call polymorphic feature. So why is it that each USB device is not one, but it can be used by computers? That's because each USB setup complies with the development specifications of the computer's USB interface, and has the same method that can be loaded and enabled by the computer, but runs its own corresponding function. This is exactly our definition of polymorphism. Suppose we have a main program that has been developed, and other developers need to extend some functions for it later, but we need to load these extended functional modules without changing the main program, which is actually to develop some plug-ins for the program. This needs to be in the main program for the extension of plug-in program to write a good interface specification, and then each plug-in can be applied to the main program only according to the specification to achieve their own functions. The program design for applying USB equipment in computer is as follows:


<?php
// Definition 1 A iUSB Interface, so that each USB All equipment complies with this specification 
interface iUSB{
function run();
}
class Computer{
// In the computer class 1 Methods can be applied to any 1 Species USB Equipment 
function useUSB($usb){
$usb->run();
}
}
?>

The following code according to the USB interface definition specification, the implementation of USB keyboard, USB mouse and USB storage 3 devices, of course, to achieve more USB settings, all in accordance with their own device functions rewritten run() Method, so every USB device has its own form after the plug-in computer starts running. The code looks like this:


<?php
// Expand 1 A USB Keyboard device, implementation usb Interface 
class Ukey implements iUSB {
function run(){
echo " Run USB Keyboard equipment <br>";
}
}
// Expand 1 A USB Mouse device, implementation usb Interface 
class Umouse implements iUSB {
function run(){
echo " Run USB Mouse device <br>";
}
}
// Expand 1 A USB Storage device, implementation usb Interface 
class Ustore implements iUSB {
function run(){
echo " Run USB Storage device <br>";
}
}
$computer =new Computer;
$computer ->useUSB(new Ukey()); // Insert for the computer 1 A usb Keyboard device, and run 
$computer ->useUSB(new Umouse()); // Insert for the computer 1 A usb Mouse device, and run 
$computer ->useUSB(new Ustore()); // Insert for the computer 1 A usb Storage device, and run 
?>

Run output:

Running USB keyboard equipment
Run the USB mouse device
Running USB storage devices

PS: The code has been modified compared with the original text, and it has been debugged and run without error ~

For more readers interested in PHP related content, please check the topics on this site: "Introduction to php Object-Oriented Programming", "Encyclopedia of PHP Array (Array) Operation Skills", "Introduction to PHP Basic Syntax", "Summary of PHP Operation and Operator Usage", "Summary of php String (string) Usage", "Introduction to php+mysql Database Operation" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: