search
HomeBackend DevelopmentPHP ProblemHow many parts does the php interface consist of?

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
?>

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)
{
}
}
?>

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool