Home > Backend Development > PHP Problem > How many parts does the php interface consist of?

How many parts does the php interface consist of?

青灯夜游
Release: 2023-02-28 16:48:01
Original
3830 people have browsed it

How many parts does the php interface consist of?

A program interface consists of a set of statements, functions, options, other forms that express the structure of a program, and data provided by the program or programming language used by the programmer

Characteristics of PHP interface

1. The methods of the interface must be public.

2. Interface methods are abstract by default, so do not add abstract in front of the method name.

3. Interfaces can define constants, but cannot define member attributes. The definition and usage of constants are the same as those in classes.

4. A class can implement multiple interfaces (equivalent to integrating multiple functions into one, such as a mobile phone that implements the functions of PHS, MP3, and MP4)

5. Interfaces can also be inherited interface.

Definition and calling of interface

<?php
interface face1
{
const param = &#39;test&#39;;
public function show();
}
class test implements face1
{
public function show()
{
echo "interface is run<br>";
}
}
$face = new test();
echo $face->show();         //inerface is run
echo face1::param;           //test
?>
Copy after login

Note: In the above example, one thing to note is that the method name of the interface is show, and the class that inherits the interface must have the show method. , otherwise an error will be reported. In other words, the methods of the interface are fake, and what really works are the methods in the inherited class. Because of this, I think the interface is somewhat similar to the abstract class of PHP.

Strict parameter constraints

<?php
interface face1
{
public function show(show $show);
}
// 显示正常
class test implements face1
{
public function show(show $show)
{
echo "asdfasdf";
}
}
// 报fatal错误
class test2 implements face1
{
public function show(aaa $aaa)
{
}
}
?>
Copy after login

Explanation: The above example reports a fatal error. Why does it report a fatal error? The reason is that the parameter passed is aaa $aaa, not show $show. In an interface class that inherits, when calling a method of the interface, the parameters passed must match the parameter names in the interface. Otherwise, an error will be reported.

Recommended learning: php programming introductory tutorial

The above is the detailed content of How many parts does the php interface consist of?. For more information, please follow other related articles on the PHP Chinese website!

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