search
Homephp教程php手册php面向对象全攻略 (十四) php5接口技术

php面向对象全攻略 (十四) php5接口技术

Jun 13, 2016 pm 12:21 PM
phpphp5andComplete strategyobjecttechnologyinterfaceprogrammingFor

20.PHP5 接口技术
PHP 与大多数面向对象编程语言一样,不支持多重继承.也就是说每个类只能继承一个父
类。为了解决这个问题,PHP 引入了接口,接口的思想是指定了一个实现了该接口的类必须
实现的一系列方法。接口是一种特殊的抽象类,抽象类又是一种特殊的类,所以接口也是一
种特殊的类,为什么说接口是一种特殊的抽象类呢?如果一个抽象类里面的所有的方法都是
抽象方法,那么我们就换一种声明方法使用“接口”;也就是说接口里面所有的方法必须都是
声明为抽象方法,另外接口里面不能声明变量,而且接口里面所有的成员都是public 权限的。
所以子类在实现的时候也一定要使用public 权限实限。
声明一个类的时候我们使用的关键字是“class”,而接口一种特殊的类,使用的关键字
是“interface”;
类的定义:class 类名{ … } ,接口的声明:interface 接口名{ … }
代码片段

复制代码 代码如下:



abstract class Demo{
var $test;
abstract function fun1();
abstract function fun2();
function fun3(){
… .
}
}
$demo=new Demo(); //抽象类为能产生实例对象,所以这样做是错的,实例化对象交给子类
class Test extends Demo{
function fun1(){

}
function fun2(){

}
}
$test=new Test(); //子类可以实例化对象,因为实现了父类中所有抽象方法
?>


上例中定义了一个接口“one”,里面声明了两个抽象方法“fun1”和“fun2”,因为接
口里面所有的方法都是抽象方法,所以在声明抽象方法的时候就不用像抽象类那样使用
“abstract”这个关键字了,默认的已经加上这个关键字,另外在接口里边的“public”这个访
问权限也可以去掉,因为默认就是public 的,因为接口里所有成员都要是公有的,所在对于
接口里面的成员我们就不能使用“private”的和“protected”的权限了,都要用public 或是默
认的。另外在接口里面我们也声明了一个常量“constant“, 因为在接口里面不能用变量成
员,所以我们要使用const 这个关键字声明。
因为接口是一种特殊的抽象类,里面所有的方法都是抽象方法,所以接口也不能产生实
例对象;它也做为一种规范,所有抽象方法需要子类去实现。
我们可以使用“extends“关键字让一个接口去继承另一个接口;
代码片段

复制代码 代码如下:


//使用”extends”继承另外一个接口
interface Two extends One{
function fun3();
function fun4();
}
?>


而我们定义一接口的子类去实现接口中全部抽象方法使用的关键字是“implements”,而
不是我们前面所说的“extends”;
代码片段

复制代码 代码如下:


//使用“implements”这个关键字去实现接口中的抽象方法
class Three implements One{
function fun1(){
… .
}
function fun2(){
… .
}
}
//实现了全部方法,我们去可以使用子类去实例化对象了
$three=new Three();
?>


我们也可以使用抽象类,去实现接口中的部分抽象方法,但要想实例化对象,这个抽象
类还要有子类把它所有的抽象方法都实现才行;
在前面我们说过,PHP 是单继承的,一个类只能有一父类,但是一个类可以实现多个接
口,就相当于一个类要遵守多个规范,就像我们不仅要遵守国家的法律,如果是在学校的话,
还要遵守学校的校规一样;
代码片段

复制代码 代码如下:


//使用implements实现多个接口
class Four implemtns 接口一, 接口二, … .{
//必须把所有接口中的方法都要实现才可以实例化对象。
}
?>


PHP 中不仅一个类可以实现多个接口,也可以在继承一个类的同时实现多个接口,一定
要先继承类再去实现接口;
代码片段

复制代码 代码如下:


//使用extends继承一个类,使用implements实现多个接口
class Four extends 类名一implemtns 接口一, 接口二, … .{
//所有接口中的方法都要实现才可以实例化对象
… … … ..
}
?>

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version

EditPlus Chinese cracked version

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