Home >Backend Development >PHP Tutorial >Summary of PHP interface technical definition, functions and usage examples

Summary of PHP interface technical definition, functions and usage examples

伊谢尔伦
伊谢尔伦Original
2017-07-03 09:35:071361browse

This article mainly introduces the PHP interface technology, and analyzes the basic concepts, functions, definitions, usage methods and related Notes of the PHP interface in the form of examples. Friends in need You can refer to the following

1. The interface is a special abstract class, why do you say that? If all the methods in an abstract class are abstract methods, then we call it an "interface".

2. In addition, variables cannot be declared in the interface.

3. All members in the interface have public permissions. All subclasses must also use public permissions when implementing them.

4. When declaring a class, we use the keyword "class", and when declaring an interface, we use the keyword "interface".

<?php
//定义一个接口使用interface关键字,“One”为接口名称
interface One{
  //定义一个常量
  const constant = &#39;constant value&#39;;
  //定义一个抽象方法fun1
  public function fun1();
  //定义了抽象方法fun2
  public function fun2();
}
?>

5. Because all methods in the interface are abstract methods, there is no need to use the "abstract" keyword when declaring abstract methods like abstract classes. This keyword is already added by default.

6. The access permission in the interface must be public. The default is public. "private" and "protected" permissions cannot be used.

7. The interface is a special abstract class, and all methods in it are abstract methods, so the interface cannot generate instances Objects.

8. We can use the "extends" keyword to let one interface inherit another interface.

interface Two extends One{
  function fun3();
  function fun4();
}

9. When we define a subclass of an interface to implement all abstract methods in the interface, the keyword used is "implements" instead of "extends" as we mentioned before.

class Three implements Two{
  function fun1() {
    ;
  }
  function fun2() {
    ;
  }
  function fun3(){
    ;
  }
  function fun4() {
    ;
  }
}
$three = new Three();
$three->fun1();

10.PHP is single inheritance. A class can only have one parent class, but a class can implement multiple interfaces, which is equivalent to a class having to comply with multiple specifications. To use implements to implement multiple interfaces, all methods in the interface must be implemented before the object can be instantiated.

11.PHP can not only implement multiple interfaces, but also implement multiple interfaces while inheriting a class. You must first inherit the class and then implement the interface.

<?php
//使用extends继承一个类,使用implements实现多个接口
class Test extends 类名一 implements 接口一,接口二,...{
//所有接口中的方法都要实现才可以实例化对象
  ......
}

The above is the detailed content of Summary of PHP interface technical definition, functions and usage examples. 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