Interface
PHP classes are single inheritance, that is, multiple inheritance is not supported. When a class requires the functions of multiple classes, inheritance is powerless. , for this reason PHP has introduced interface technology
If all the methods in an abstract class are abstract methods, and no variables are declared, and all members in the interface All have public permissions, so this special abstract class is called an interface
The interface is defined using the interface keyword, and implements are used to implement the interface methods, and must be fully implemented
Implement an interface
The following is a practical example of PHP interface implementation. Here, an interface named IPillage is created and implemented. The IPillage interface is as follows:
interface IPillage { function emptyBakAccount(); function burnDocument(); }
Then pass The Executive class implements this interface:
class Executive extends Employee implements IPillage { private $totalStockOptions; function emptyBankAccount() { echo "Call CFO and ask to transfer funds to Swiss bank account."; } function burnDocuments() { echo "Torch the office suite."; } }
There can be an Assistant class that implements this interface:
class Assistant extends Employee implements IPillage { function takeMome() { echo "Taking memo..."; } function emptyBankAccount() { echo "Go on shopping spree with office credit card."; } function burnDocuments() { echo "Start small fire in the trash can."; } }
As you can see, the interface is particularly useful. Because, although they define how many methods are required to occur for a certain behavior, as well as the names of each methods, interfaces allow different classes to implement these methods in different ways. In this example, for the method of burning files, the Assistdnt class just burns the files in the trash can, while the Executive class does it in a more extreme way (burning its office).
Implementing multiple interfacesIt would be unfair if we allowed outside contractors to encroach on the company, after all the company was built with the efforts of all full-time employees. That is, how do you provide employees with jobs and encroach on company functions, while limiting contractors to only completing the required tasks? The solution is to break these tasks into several tasks and then implement the necessary multiple interfaces. PHPS supports this feature. Consider the following example:
<?php interface IEmployee{...} interface IDeveloper{...} interface IPillage{...} class Employee implements IEmployee,IDeveloper,iPillage { ... } class Contractor implements IEmployee,IDeveloper { ... } ?>
The difference between abstract classes and interfacesAn interface is a special abstract class and can also be regarded as a specification of a model. The general difference between an interface and an abstract class is as follows:
If a subclass implements an interface, it must implement all methods in the interface (whether needed or not); if it inherits an abstract class, it only needs to implement the required methods.
Abstract classes can only be inherited singly. When a subclass needs to implement functions that need to inherit from multiple parent classes, it must use the interface
<?php
/**
* 声明接口Demo
*
*/
interface Demo
{
const NAME = "wangzhengyi";
const AGE = 25;
function fun1 (); // 声明方法默认是public abstract
function fun2 ();
}
/**
* 声明接口的继承Demo2
*
* @author wzy
*
*/
interface Demo2 extends Demo
{
function fun3 ();
function fun4 ();
}
/**
* 声明接口Demo3
*
* @author wzy
*
*/
interface Demo3
{
function fun5 ();
function fun6 ();
}
/**
* 声明父类ParentClass
*
* @author wzy
*
*/
class ParentClass
{
function fun7 ();
}
/**
* 子类必须实现接口中所有的方法
*
* @author wzy
*
*/
class ChildClass extends ParentClass implements Demo2, Demo3
{
function fun1 ();
function fun2 ();
function fun3 ();
function fun4 ();
function fun5 ();
function fun6 ();
}
The above is the detailed content of How are interfaces used in php?. For more information, please follow other related articles on the PHP Chinese website!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version
Visual web development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool