Application of PHP Object-oriented polymorphism

Source: Internet
Author: User

Polymorphism is another important feature of object-oriented three major features, except encapsulation and inheritance. It shows the function of dynamic binding, also known as "the same name is different". Polymorphic functionality allows the software to be fully extensible during development and maintenance. In fact, the most straightforward definition of polymorphism is to allow different classes of objects with inheritance relationships to be called on member functions of the same name, producing different reaction effects. Polymorphism refers to the ability of a program to handle multiple types of objects, and in PHP, a polymorphic value is a method rewrite. A method override is a subclass in which some methods in a parent class can be re-modified to have their own characteristics. Overriding requires subclasses to have the same method name as the parent class, which can be normalized by declaring an abstract class or an interface.
We use the computer USB device to introduce the application of the object-oriented polymorphism, the current type of USB settings only we have used it, I think there are more than 10 kinds of it. For example, USB mouse, USB keyboard, USB storage device, and so on, the external devices of these computers are connected to the computer through the USB interface, and are called by the computer and started to run. That is, while the computer is working properly, it does not plug into a different USB device, it is the same function as the computer extension, which is what we call the polymorphic feature. So why each USB device is different, but can be computer applications it? That's because each USB setting adheres to the computer's USB interface development specifications, all with the same methods that can be loaded into and enabled by the computer, but running their respective functions. This is also our definition of polymorphism, assuming that we have a main program has been developed, we need to later by other developers to extend some functionality for it, but need not change the main program on the basis can be loaded into these extended function modules, in fact, to develop some plug-ins for the program. This needs to be in the main program to write the interface specification for the extended plug-in program, and then each plug-in only according to the specification to implement their own functions, can be applied to the main program. The program design for applying a USB device to a computer is as follows:

123456789101112131415161718 <?php//定义一个USB接口,让每个USB设备都遵守这个规范interface USB{function run();}class Computer{//计算机类中的一个方法可以应用任何一种USB设备function useUSB($usb){$usb->run();}} $computer =new Computer;$computer ->useUSB(new Ukey()); //为计算机插入一个usb键盘设备,并运行$computer ->useUSB(new Umouse()); //为计算机插入一个usb鼠标设备,并运行$computer ->useUSB(new Ustore()); //为计算机插入一个usb存储设备,并运行?>

The following code according to the specifications of the USB interface definition, the implementation of USB keyboard, USB mouse and USB storage Three devices, of course, can go to achieve more USB settings, according to the function of their own device rewrite the run () method, so after the plug-in computer started running every USB device has its own shape. The code looks like this:

123456789101112131415161718192021 <?php//扩展一个USB键盘设备,实现usb接口class Ukey implements USB {function run(){echo "运行USB键盘设备<br>";}}//扩展一个USB鼠标设备,实现usb接口class Umouse implements USB {function run(){echo "运行USB鼠标设备<br>";}}//扩展一个USB存储设备,实现usb接口class Ustore implements USB {function run(){echo "运行USB存储设备<br>";}}?>

>> This article fixed link: http://php.ncong.com/php_course/oop/duotai.html

>> reprint Please specify: Ntshongwana PHP July 25, 2014 Yuncon PHP Learning tutorial Published

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.