PHP object-oriented-interfaces and Polymorphism

Source: Internet
Author: User
Foreword when I checked my undergraduate C ++ exam today, I found that some students still did not understand the three main characteristics of object-oriented programming, here, I will use PHP to explain the interface and polymorphism. It also serves as a self-learning interface. The PHP class is a single inheritance, that is, it does not support multi-inheritance, when a class requires multiple class functions, inheritance is powerless. For this reason, PHP introduces the interface technology. If all methods in an abstract class are abstract methods and no variables are declared, in addition, all the Members in the interface have the public permission, so this special abstract class is called the interface using the interface keyword definition and using implements to implement the interface method, the difference between an abstract class and an interface must be fully implemented. An interface is a special abstract class and can also be seen as a model specification. The interface and abstract class are roughly different as follows:
  1. If a subclass implements an interface, all methods in the interface must be implemented (whether or not required). If an abstract class is inherited, you only need to implement the required methods.
  2. If the method name defined in an interface is changed, all subclasses implementing this interface must synchronously update the method name. If the method name in the abstract class is changed, the method name corresponding to its subclass is not affected, but is changed to a new method.
  3. Abstract classes can only be inherited. When a subclass needs to implement a function that inherits multiple parent classes, the interface must be used.
Sample Code
<? PHP/*** declare interface demo * @ author wzy **/interface demo {const name = "wangzhengyi"; const age = 25; function fun1 (); // The default declaration method is public abstract function fun2 ();} /*** declare the inheritance of the interface demo2 *** @ author wzy **/interface demo2 extends demo {function fun3 (); function fun4 ();} /*** declare the interface demo3 *** @ author wzy **/interface demo3 {function fun5 (); function fun6 ();} /*** declare parent class parentclass ** @ author wzy **/class parentclass {function fun7 ();} /*** subclass must implement all methods in the interface ** @ author wzy **/class childclass extends parentclass implements demo2, demo3 {function fun1 (); function fun2 (); function fun3 (); function fun4 (); function fun5 (); function fun6 ();}
Polymorphism is an important feature of object-oriented design. It shows the function of dynamic binding and becomes the "same name ". The polymorphism function enables the software to achieve full extension during development and maintenance. The most direct definition of polymorphism is to let different class objects with inheritance relationships, you can call member functions with the same name to generate code with different response result polymorphism.
<? PHP/*** declare interface demo * @ author wzy **/interface demo {const name = "wangzhengyi"; const age = 25; function fun1 (); // The default declaration method is public abstract function fun2 ();} Class One implements demo {public function fun1 () {echo Demo: Name. "studied at media University of China";} public function fun2 () {echo Demo: Name. "The age is ". demo: Age ;}} Class Two implements demo {public function fun1 () {echo Demo: Name. "In Beijing lingchuangzhong and Technology Co., Ltd. Internship "; } Public function fun2 () {echo Demo: Name. "Last year's age was 24" ;}/// the same interface implements the same method, different objects, and different response results. This is the manifestation and Application of polymorphism $ one = new one (); $ one-> fun1 (); // wangzhengyi studied at $ one-> fun2 (); // The Age Of wangzhengyi is 25 echo "<br>"; $ two = new two (); $ two-> fun1 (); // wangzhengyi $ two-> fun2 (); // wangzhengyi was aged 24 last year

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.