Home  >  Article  >  Backend Development  >  How to implement multiple interfaces in php

How to implement multiple interfaces in php

藏色散人
藏色散人Original
2022-10-18 10:28:011764browse

How to implement multiple interfaces in php: 1. Create a PHP sample file; 2. Create "interface staff_i1{...} interface staff_i2{...}"; 3. Pass "class staff implements staff_i1, staff_i2{...}" method can be used to implement the interface.

How to implement multiple interfaces in php

The operating environment of this tutorial: Windows 7 system, PHP version 8.1, DELL G3 computer

How to implement multiple php interfaces

The code is as follows:

<?php 
  interface staff_i1 //接口1 
  { 
   function setID($id); 
   function getID(); 
  }   
  interface staff_i2 //接口2 
  { 
   function setName($name); 
   function getName(); 
  }     
  class staff implements staff_i1, staff_i2 //接口的实现 
  { 
   private $id; 
   private $name; 
   function setID($id) 
   { 
     $this->id = $id; 
   } 
   function getID()  
   { 
     return $this->id; 
   } 
   function setName($name)  
   { 
     $this->name = $name; 
   } 
   function getName()  
   { 
     return $this->name; 
   } 
   function otherFunc() 
   { 
    echo "Test"; 
   } 
  } 
?>

Note:

Using interface (interface), you can specify which methods a certain class must implement, but you do not need to define the specifics of these methods. content. Since interfaces, classes, and traits share a namespace, they cannot have the same name.

The interface is defined just like defining a standard class by replacing the class keyword with the interface keyword, but all methods are empty.

All methods defined in the interface must be public, which is a characteristic of the interface.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to implement multiple interfaces in php. For more information, please follow other related articles on the PHP Chinese website!

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