Home >Backend Development >PHP Tutorial >Examples to explain polymorphism in PHP object-oriented programming_php skills

Examples to explain polymorphism in PHP object-oriented programming_php skills

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-05-16 20:08:371106browse

What is polymorphism?
Polymorphism, the definition from dictionary.com is "occurring in different forms, stages or types in independent organizations or in the same organization without fundamental difference." From this definition, we can think ,Polymorphism is a programming way of describing the same ,object through multiple states or phases. In fact, its real meaning is: 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, PHP5 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's 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.

Popular understanding
The most direct definition of polymorphism is to allow objects of different classes with inheritance relationships to call member functions with the same name and produce different reaction results

Polymorphic code

<&#63;php 
   
  /** 
   * 声明接口Demo 
   * @author wzy 
   * 
   */ 
  interface Demo 
  { 
   
    const NAME = "wangzhengyi"; 
   
    const AGE = 25; 
   
    function fun1 (); // 声明方法默认是public abstract 
    function fun2 (); 
  } 
   
  class One implements Demo 
  { 
   
    public function fun1 () 
    { 
      echo Demo::NAME . "就读于中国传媒大学"; 
    } 
   
    public function fun2 () 
    { 
      echo Demo::NAME . "的年龄是" . Demo::AGE; 
    } 
  } 
   
  class Two implements Demo 
  { 
   
    public function fun1 () 
    { 
      echo Demo::NAME . "在北京灵创众和科技有限公司实习中"; 
    } 
   
    public function fun2 () 
    { 
      echo Demo::NAME . "去年的年龄是24"; 
    } 
  } 
   
  // 同一个接口,实现同一个方法,不同的对象,反应结果不同。这就是多态的表现和应用 
   
  $one = new One(); 
  $one->fun1(); // wangzhengyi就读于中国传媒大学 
  $one->fun2(); // wangzhengyi的年龄是25 
   
  echo "<br>"; 
   
  $two = new Two(); 
  $two->fun1(); // wangzhengyi在北京灵创众和科技有限公司实习中 
  $two->fun2(); // wangzhengyi去年的年龄是24 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn