What is the php object-oriented interface? how to use?

伊谢尔伦
Release: 2023-03-11 21:56:01
Original
2259 people have browsed it

Interface is a very important concept in PHP object-oriented programming. This article describes the usage of the PHP interface in more detail in the form of examples. The details are as follows:

Interface: interface

In PHP, we can specify which public external operations an object should have, which can be specified using interface.
The public method is the interface. Used to specify which public operation methods (interfaces) an object should be used for. This is also called an interface (a collection of public operation methods)
That is: interface (interface structure, public method collection)

Public method (Interface method)
Definition: A structure used to limit the public operation methods that an object must have, called interface (interface)
Syntax: To define the interface structure, use the interface keyword. What are defined in the interface are some public methods.

interface接口名
{
公共操作方法列表
}
Copy after login

Examples are as follows:

interface I_Goods
{
public function sayName();
public function sayPrice();
}
Copy after login

Note:
1.Interface methods, access rights must be public public
2.There can only be Public methods cannot have members Variables
3. The interface can only contain unimplemented methods, also called abstract methods, but do not use the abstract keyword.

The class implements the interface and uses the keyword implements to complete.

Example:

interface I_Goods
{
public function sayName();
public function sayPrice();
}
class Goods implements I_Goods
{
public function sayName()
{
}
public function sayPrice()
{
}
}
Copy after login

In this way, the class that implements the interface must implement all abstract methods in the interface. And it is certain that this method must be a public external operation method.

Multiple implementations: This function can theoretically be implemented through abstract classes, but abstract classes are not professional.
Using interfaces is more professional in terms of implementation, because php supports multiple implementations and only supports single inheritance.

Examples are as follows:

interface I_Goods
{
public function sayName();
public function sayPrice();
}
interface I_Shop
{
public function saySafe();
}
class Goods implements I_Goods , I_Shop
{
public function sayName()
{
}
public function sayPrice()
{
}
public function saySafe()
{
}
}
Copy after login

Interfaces can also be inherited
Examples are as follows:

interface I_Goods
{
public function sayName();
public function sayPrice();
}
interface I_Shop extends I_Goods
{
public function saySafe();
}
class Goods implements I_Shop
{
public function sayName()
{
}
public function sayPrice()
{
}
public function saySafe()
{
}
}
Copy after login

phpObject interface support, you can define class constants

Examples are as follows:

interface I_Goods
{
const PAI = 3.14;
public function sayName();
public function sayPrice();
}
interface I_Shop extends I_Goods
{
public function saySafe();
}
class Goods implements I_Shop
{
public function sayName()
{
}
public function sayPrice()
{
}
public function saySafe()
{
}
}
echo Goods::PAI;
Copy after login

Run output: 3.14

The above is the detailed content of What is the php object-oriented interface? how to use?. 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