PHP interface technology examples and detailed explanations with pictures and texts

墨辰丷
Release: 2023-03-28 11:46:01
Original
2010 people have browsed it

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

The details are as follows:

1. 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();
}
?>
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 has been 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. 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 the abstract methods in the interface, the keyword used is "implements", not what we said before "extends".

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 one class. There are multiple specifications to follow. 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

The above is the entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

php-fpm.conf Configuration Instructions

PHPRealizing multi-process and multi-threading

##PHPSome basic tags to learn

The above is the detailed content of PHP interface technology examples and detailed explanations with pictures and texts. For more information, please follow other related articles on the PHP Chinese website!

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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template