Explanation of Inheritance Definition and Usage of PHP Object Oriented Programming Interface

  • 2021-11-13 00:57:22
  • OfStack

This paper describes the inheritance definition and usage of PHP object-oriented programming interface with examples. Share it for your reference, as follows:

In PHP5, an interface can inherit from another interface. In this way, code reuse is more effective. Note that inheritance keywords are used only between interfaces extends . Class implementation interfaces must implement their abstract methods, using implementation keywords implements .

The following example defines interface User, User with two abstract methods getName and setName. The interface VipUser is defined, which inherits from the interface User, and the method getDiscount related to discount is added.

Finally, the class Vip is defined and the VipUser interface is implemented. And three methods are implemented.


<?php
interface User {
  public function getName();
  public function setName($_name);
}
interface VipUser extends User {
  public function getDiscount(); // Added an abstract method to get discounts .
}
class Vip implements VipUser {
  private $name;
  private $discount = 0.8;//  Define discount variables 
  public function getName(){ // Realization getName Method 
    return $this->name;
  }
  public function setName($_name){// Realization setName Method 
    $this->name = $_name;
  }
  public function getDiscount(){// Implement discount method .
    return $this->discount;
  }
}
?>

Interfaces can implement multiple inheritance, which is very special about interfaces. Note the following code and usage.


<?php
interface User {
  public function getName();
  public function setName($_name);
}
interface Administrator {
  public function setNews($_news);
}
// Note the multiple inheritance here .
interface NewsAdministrator extends User,Administrator{
}
class NewsAdmin implements NewsAdministrator { // Implementation interface 
  public function getName(){
    //.........
  }
  public function setName($_name){
    //.........
  }
  public function setNews($_news){
    //.........
  }
}
?>

An abstract class implements an interface that does not implement an abstract method in it, but delivers the implementation of an abstract method to a concrete class that can be instantiated for processing.


<?php
interface User {
  public function getName();
  public function setName($_name);
}
//AbstractNormalUser  Only realized  User Interface in the 1 Methods ,
abstract class AbstractNormalUser{
  protected $name;
  public function getName(){
    return $this->name;
  }
}
// Another interface is implemented here 1 Methods .
class NormalUser extends AbstractNormalUser {
  public function setName($_name){
    $this->name = $_name;
  }
}
$normalUser = new NormalUser();
$normalUser->setName("tom");
echo "name is ".$normalUser->getName();
?>

Run results:

name is tom

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: