search
HomeBackend DevelopmentPHP Tutorialphp面向对象全攻略 (十四) php5接口技术_PHP

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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

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

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download

Atom editor mac version download

The most popular open source editor