Interface Usage of PHP Object Oriented Programming

  • 2021-07-13 04:47:45
  • OfStack

Interface is a very important concept in PHP object-oriented programming. In this paper, the usage of PHP interface is described in detail in the form of examples. The details are as follows:

Interface: interface

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

Public method (interface method)
Definition: A structure used to define the common operation methods that an object must have, called an interface (interface)
Syntax: Define the interface structure using the interface keyword. There are 1 public methods defined in the interface.


interface Interface name 
{
 List of public action methods 
}

Examples are as follows:


interface I_Goods
{
public function sayName();
public function sayPrice();
}

Note:
1. Interface method, access must be public public
2. There can only be public methods in the interface, and no member variables can exist
3. The interface can only contain unimplemented methods, also called abstract methods, but the abstract keyword is not used.

Class to implement the interface, using the keyword implements.

Examples:


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

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

Multi-implementation: This function can be realized by abstract classes in theory, but abstract classes are unprofessional.
Using interfaces is more professional, 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()
{
}
}

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

Support of php object interface, which 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;

Run output: 3.14


Related articles: