Table of Contents
1、php 接口类:interface 
2、php 抽象类 : abstract 
Home Backend Development PHP Tutorial php抽象类和接口的区别

php抽象类和接口的区别

Jun 20, 2016 pm 01:03 PM
php interface

1、php 接口类:interface 

其实他们的作用很简单,当有很多人一起开发一个项目时,可能都会去调用别人写的一些类,那你就会问,我怎么知道他的某个功能的实现方法是怎么命名的呢,这个时候php接口类就起到作用了,当我们定义了一个接口类时,它里面的方式是下面的子类必须实现的,比如 : 
代码如下:
interface Shop <br />{ <br />public function buy($gid); <br />public function sell($gid); <br />public function view($gid); <br />} 
Copy after login


我声明一个shop接口类,定义了三个方法:买(buy),卖(sell),看(view),那么继承此类的所有子类都必须实现这3个方法少一个都不行,如果子类没有实现这些话,就无法运行。实际上接口类说白了,就是一个类的模板,一个类的规定,如果你属于这类,你就必须遵循我的规定,少一个都不行,但是具体你怎么去做,我不管,那是你的事,如:
代码如下:
class BaseShop implements Shop <br />{ <br />public function buy($gid) <br />{ <br />echo('你购买了ID为 :'.$gid.'的商品'); <br />} <br />public function sell($gid) <br />{ <br />echo('你卖了ID为 :'.$gid.'的商品'); <br />} <br />public function view($gid) <br />{ <br />echo('你查看了ID为 :'.$gid.'的商品'); <br />} <br />} 
Copy after login


你想想,在一个多人合作的大项目里面,有了接口类是多么的方便,这样你就不用去问别人,你的某某功能的方法名是什么了,当然如果你们喜欢这样我也没有办法。
结论 : 接口类就是一个类的领导者,指明方向,子类必须完成它指定方法。

2、php 抽象类 : abstract

其实抽象类和接口类有一部分很像,记得在哪里看见这样一句话,抽象类就把类像的部分抽出来,这句看上去很搞笑,其实它说出了抽象类的真理,抽象类的作用是,当你发现你的很多类里面用很多方法你不断的在重复写,那你就可以考虑使用抽象类了,你可能会说“我不是可以重写一个类每个公共类我个实例化一个这个公共类,调用相同的方法就可以了”,这里是可以,实际上抽象类做的工作也就是这个,不过他省去了你实例化的这个步骤,让你就像直接调用本类方法一样方便,而且你还可以重载这个方法。如:
代码如下:
abstract class BaseShop <br />{ <br />public function buy($gid) <br />{ <br />echo('你购买了ID为 :'.$gid.'的商品'); <br />} <br />public function sell($gid) <br />{ <br />echo('你卖了ID为 :'.$gid.'的商品'); <br />} <br />public function view($gid) <br />{ <br />echo('你查看了ID为 :'.$gid.'的商品'); <br />} <br />} <br />class BallShop extends BaseShop <br />{ <br />var $itme_id = null; <br />public function __construct() <br />{ <br />$this->itme_id = 2314; <br />} <br />public function open() <br />{ <br />$this->sell($this->itme_id); <br />} <br />} 
Copy after login


这里是一个例子,想上面一样我定义了一个商店类,抽出了它所有像的部分,买(buy),卖(sell),看(view),并且抽象类里都实现了这些方法,那么继承它的子类就自动获得了这些方法,子类就做它自己独特的东西,介绍代码的重复,提高复用性。 

结论:

 抽象类就是一个类的服务提供商,拥有众多服务,你不用必须用,当需要的时候你来用就可以,如果你觉得不提供服务不满意,你还可以自己来做。

区别:
1、对接口的使用是通过关键字implements。对抽象类的使用是通过关键字extends。当然接口也可以通过关键字extends继承。

2、接口中不可以声明成员变量(包括类静态变量),但是可以声明类常量。抽象类中可以声明各种类型成员变量,实现数据的封装。(另JAVA接口中的成员变量都要声明为public static final类型)

3、接口没有构造函数,抽象类可以有构造函数。

4、接口中的方法默认都是public类型的,而抽象类中的方法可以使用private,protected,public来修饰。

5、一个类可以同时实现多个接口,但一个类只能继承于一个抽象类。

抽象类还是接口。
如果要创建一个模型,这个模型将由一些紧密相关的对象采用,就可以使用抽象类。如果要创建将由一些不相关对象采用的功能,就使用接口。
如果必须从多个来源继承行为,就使用接口。
如果知道所有类都会共享一个公共的行为实现,就使用抽象类,并在其中实现该行为。


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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use php interface and ECharts to generate visual statistical charts How to use php interface and ECharts to generate visual statistical charts Dec 18, 2023 am 11:39 AM

In today's context where data visualization is becoming more and more important, many developers hope to use various tools to quickly generate various charts and reports so that they can better display data and help decision-makers make quick judgments. In this context, using the Php interface and ECharts library can help many developers quickly generate visual statistical charts. This article will introduce in detail how to use the Php interface and ECharts library to generate visual statistical charts. In the specific implementation, we will use MySQL

How to combine ECharts and php interface to realize dynamic update of statistical charts How to combine ECharts and php interface to realize dynamic update of statistical charts Dec 17, 2023 pm 03:47 PM

How to combine ECharts and PHP interfaces to implement dynamic updates of statistical charts Introduction: Data visualization plays a vital role in modern applications. ECharts is an excellent JavaScript chart library that can help us easily create various types of statistical charts. PHP is a scripting language widely used in server-side development. By combining ECharts and PHP interfaces, we can realize dynamic updating of statistical charts, so that charts can be automatically updated according to changes in real-time data. Book

How to display real-time statistical charts through ECharts and php interfaces How to display real-time statistical charts through ECharts and php interfaces Dec 17, 2023 pm 04:35 PM

How to display real-time statistical charts through ECharts and PHP interfaces. With the rapid development of the Internet and big data technology, data visualization has become an important part. As an excellent open source JavaScript data visualization library, ECharts can help us display various statistical charts simply and efficiently. This article will introduce how to display real-time statistical charts through ECharts and PHP interfaces, and provide relevant code examples. 1. Preparation Before starting, we need to do some preparations

What are SPL interfaces (e.g., Iterator, Countable, ArrayAccess) and why use them? What are SPL interfaces (e.g., Iterator, Countable, ArrayAccess) and why use them? Apr 04, 2025 am 12:01 AM

The SPL interface includes Iterator, Countable and ArrayAccess in PHP. 1. The Iterator interface makes the object traversable and defines the current(), key(), next(), rewind() and valid() methods. 2. The Countable interface allows the object to report the number of elements and defines the count() method. 3. The ArrayAccess interface allows objects to be accessed and modified like arrays, and defines offsetExists(), offsetGet(), offsetSet() and offsetUnset() methods. These interfaces improve code efficiency and maintainability.

In-depth understanding of the definition and use of PHP interfaces In-depth understanding of the definition and use of PHP interfaces Mar 24, 2024 am 08:45 AM

Deeply understand the definition and usage of PHP interfaces. PHP is a powerful server-side scripting language that is widely used in the field of web development. In PHP, interface is an important concept that can be used to define the specifications of a set of methods without caring about the specific implementation of the methods. This article will delve into the definition and use of PHP interfaces and provide specific code examples. 1. What is an interface? In object-oriented programming, an interface is an abstract concept that defines the specification of a set of methods, but has no specific

How to implement data verification and verification of statistical charts through ECharts and php interfaces How to implement data verification and verification of statistical charts through ECharts and php interfaces Dec 18, 2023 pm 02:13 PM

How to implement data verification and verification of statistical charts through ECharts and PHP interfaces. As the demand for data visualization increases, ECharts has become a very popular data visualization tool. As a common back-end scripting language, PHP is also widely used in web development. This article will introduce how to implement data verification and verification of statistical charts through ECharts and PHP interfaces, and provide specific code examples. First, we need to understand ECharts. ECharts is an open source software developed by Baidu

How to generate interactive statistical charts through the php interface and ECharts How to generate interactive statistical charts through the php interface and ECharts Dec 18, 2023 pm 01:07 PM

In modern applications, visualization of data is becoming more and more popular. Statistical charts are a great way to visualize data and can easily help users understand trends in data. ECharts is a powerful front-end chart framework that provides rich chart types and interactive functions. Php is a very popular backend language that makes it easy to generate dynamic content and interfaces. In this article, we will introduce how to use the PHP interface and ECharts to generate interactive statistical charts, and provide specific code examples. one,

How to use php interface and ECharts to implement data filtering and filtering of statistical charts How to use php interface and ECharts to implement data filtering and filtering of statistical charts Dec 17, 2023 pm 05:36 PM

How to use the PHP interface and ECharts to implement data screening and filtering of statistical charts requires specific code examples. In data visualization, using statistical charts is a common way to display data. In practical applications, data often needs to be screened and filtered to meet different needs. The PHP interface and ECharts are two widely used tools through which data filtering and filtering of statistical charts can be implemented. The following will use an example to demonstrate how to use the PHP interface and ECharts implementation

See all articles