Home > php教程 > PHP开发 > body text

Detailed explanation of php interface technology examples

黄舟
Release: 2016-12-14 10:40:27
Original
1198 people have browsed it

The examples in this article describe PHP interface technology. Share it with everyone for your reference, the details are as follows:

1. Interface is a special abstract class, why do you say this? 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();
}
?>
Copy after login

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. An interface is a special abstract class. All methods in it are abstract methods, so the interface cannot produce instance objects.

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

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

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();
Copy after login

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 接口一,接口二,...{
//所有接口中的方法都要实现才可以实例化对象
  ......
}
Copy after login

I hope this article will be helpful to everyone in PHP programming. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template