PHP object-oriented programming interface usage, php object-oriented programming_PHP tutorial

WBOY
Release: 2016-07-13 10:20:34
Original
932 people have browsed it

PHP object-oriented programming interface usage, php object-oriented programming

Interface is a very important concept in PHP object-oriented programming. This article describes the usage of the PHP interface in more detail with 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.
Public methods are interfaces. 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 methods (interface methods)
Definition: A structure used to limit the public operation methods that an object must have, called an interface
Syntax: Define the interface structure and 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 in the interface and no member variables
3. The interface can only contain unimplemented methods, also called abstract methods, but the abstract keyword is not used.

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

PHP object 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

For answers to questions about the interface between PHP and other object-oriented programming

The PHP interface is not used in conjunction with other programming languages, but to develop a template for future development. Everyone can just follow this template.
For example, regarding database operations. The interface can be written like this, setting the four methods of insert, update, select, and delete. The database is nothing more than four operations of add, delete, modify, and check.
Other programmers only need to implement (inplements) this interface and write programs according to the ideas of these four methods. Once written, the database interface is implemented.

In this way, the chief software designer only needs to give the software a few things (interfaces) to do, and the following programmers can do it according to these few things. Are interfaces very advantageous in systems developed by multiple people?

As for common API interfaces, in fact, to put it simply, they are method function classes. We only need to call the methods written by others.

Why does object-oriented PHP use interfaces and abstract classes, and what roles do they play? Let’s introduce it in detail

Abstract class:
is used for inheritance. It cannot be instantiated. It is used to set specifications. Subclasses must implement all the abstract methods of the parent class.
Interface:
can be understood as a more strict abstraction. Class
First, like an abstract class, specifications can be set. Because interfaces have characteristics, to implement the interface, all methods inside must be implemented. In this way, the project manager can set a specification in the interface, which functions should be implemented
Second, PHP uses single inheritance. A class can only have one parent class. In order to solve this problem, interfaces appeared. One class can implement multiple interfaces

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/866658.htmlTechArticleInterface usage in PHP object-oriented programming, php object-oriented programming interface is very important in PHP object-oriented programming a concept. This article explains it in detail in the form of examples...
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