详细解读PHP中接口的应用
这篇文章主要介绍了PHP中接口的应用,是PHP入门学习中的基础知识,需要的朋友可以参考下
接口
PHP类是单继承,也就是不支持多继承,当一个类需要多个类的功能时,继承就无能为力了,为此PHP引入了接口技术
如果一个抽象类里面的所有方法都是抽象方法,且没有声明变量,而且接口里面所有的成员都是public权限的,那么这种特殊的抽象类就叫接口
接口使用interface关键字定义,并使用implements来实现接口的方法,且必须完全实现
实现一个接口
下面给出PHP接口实现的一个实际例子,在此创建并实现了一个名为IPillage的接口,IPillage接口如下:
interface IPillage { function emptyBakAccount(); function burnDocument(); }
然后通过Executive类实现此接口:
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."; } }
因为公司中所有级别的人都能进行侵占,所以可以有Assistant类实现此接口:
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."; } }
可以看到,接口特别有用。因为,虽然它们定义了发生某一行为需要多少个方法,以及各个方法的名字,但接口允许不同的类,以不同的方式来实现这些方法。在这个例子中,对于烧文件的方法,Assistdnt类只是把文件在垃圾筒里烧掉,,而Executive类则通过更过分的方式来做到(将它的办公室烧掉)。
实现多个接口
如果我们允许外来承包商侵占公司是不公平的,毕竟公司是在所有全职员工的努力之下建立的。就是说,怎样为员工提供工作和侵占公司的功能,而限制承包商只能完成所需的任务呢?解决的办法是将这些任务分成几项任务,然后实现必要的多个接口。PHPS 支持这个特性。考虑如下例子:
抽象类和接口的区别
接口是一个特殊的抽象类,也可以看作是一个模型的规范。接口与抽象类大致区别如下:
一个子类如果implements一个接口,就必须实现接口中的所有方法(不管是否需要);如果继承一个抽象类,只需要实现需要的方法即可
如果一个接口中定义的方法名改变了,那么所有实现此接口的子类需要同步更新方法名;而抽象类中如果方法名改变了,其子类对应的方法名将不受影响,只是变成了一个新的方法而已
抽象类只能单继承,当一个子类需要实现的功能需要继承多个父类时,就必须使用接口
代码示例

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

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Deleted something important from your home screen and trying to get it back? You can put app icons back on the screen in a variety of ways. We have discussed all the methods you can follow and put the app icon back on the home screen. How to Undo Remove from Home Screen in iPhone As we mentioned before, there are several ways to restore this change on iPhone. Method 1 – Replace App Icon in App Library You can place an app icon on your home screen directly from the App Library. Step 1 – Swipe sideways to find all apps in the app library. Step 2 – Find the app icon you deleted earlier. Step 3 – Simply drag the app icon from the main library to the correct location on the home screen. This is the application diagram

The role and practical application of arrow symbols in PHP In PHP, the arrow symbol (->) is usually used to access the properties and methods of objects. Objects are one of the basic concepts of object-oriented programming (OOP) in PHP. In actual development, arrow symbols play an important role in operating objects. This article will introduce the role and practical application of arrow symbols, and provide specific code examples to help readers better understand. 1. The role of the arrow symbol to access the properties of an object. The arrow symbol can be used to access the properties of an object. When we instantiate a pair

The Linuxtee command is a very useful command line tool that can write output to a file or send output to another command without affecting existing output. In this article, we will explore in depth the various application scenarios of the Linuxtee command, from entry to proficiency. 1. Basic usage First, let’s take a look at the basic usage of the tee command. The syntax of tee command is as follows: tee[OPTION]...[FILE]...This command will read data from standard input and save the data to

Introduction to PHP interface and how it is defined. PHP is an open source scripting language widely used in Web development. It is flexible, simple, and powerful. In PHP, an interface is a tool that defines common methods between multiple classes, achieving polymorphism and making code more flexible and reusable. This article will introduce the concept of PHP interfaces and how to define them, and provide specific code examples to demonstrate their usage. 1. PHP interface concept Interface plays an important role in object-oriented programming, defining the class application

The Go language is an open source programming language developed by Google and first released in 2007. It is designed to be a simple, easy-to-learn, efficient, and highly concurrency language, and is favored by more and more developers. This article will explore the advantages of Go language, introduce some application scenarios suitable for Go language, and give specific code examples. Advantages: Strong concurrency: Go language has built-in support for lightweight threads-goroutine, which can easily implement concurrent programming. Goroutin can be started by using the go keyword

The wide application of Linux in the field of cloud computing With the continuous development and popularization of cloud computing technology, Linux, as an open source operating system, plays an important role in the field of cloud computing. Due to its stability, security and flexibility, Linux systems are widely used in various cloud computing platforms and services, providing a solid foundation for the development of cloud computing technology. This article will introduce the wide range of applications of Linux in the field of cloud computing and give specific code examples. 1. Application virtualization technology of Linux in cloud computing platform Virtualization technology

Interfaces and abstract classes are used in design patterns for decoupling and extensibility. Interfaces define method signatures, abstract classes provide partial implementation, and subclasses must implement unimplemented methods. In the strategy pattern, the interface is used to define the algorithm, and the abstract class or concrete class provides the implementation, allowing dynamic switching of algorithms. In the observer pattern, interfaces are used to define observer behavior, and abstract or concrete classes are used to subscribe and publish notifications. In the adapter pattern, interfaces are used to adapt existing classes. Abstract classes or concrete classes can implement compatible interfaces, allowing interaction with original code.

LinuxBashrc is a configuration file in the Linux system, used to set the user's Bash (BourneAgainShell) environment. The Bashrc file stores information such as environment variables and startup scripts required for user login, and can customize the user's Shell environment. In the Linux system, each user has a corresponding Bashrc file, which is located in a hidden folder in the user's home directory. The main functions of the Bashrc file are as follows: setting up the environment
