PHP接口:它们如何在实践中可用? [英] PHP Interfaces: How are they usable in practice?

查看:147
本文介绍了PHP接口:它们如何在实践中可用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会先说我知道PHP接口如何工作,以及如何使用他们。

I'll start by saying that I know how PHP interfaces work and how to "use" them.

我的问题是,它们如何在现实生活中变得有用?

My question is rather; how do they become useful in real life applications?

我已经写了3年以上的PHP,从来没有感觉到需要 一个接口。

I've been writing PHP for over 3 years now and have never felt the need for an interface. I am writing interfaces more for good practice than for a particular purpose.

推荐答案

我将提供一个示例,其中我已经使用了

I'll provide an example where I've used interfaces in my own real-world experience.

当你需要定义类似插件架构的东西时,接口是非常有用的。假设您的应用程序接受身份验证插件,允许您的最终用户实施者与他们自己的内部验证基础结构(LDAP,Shibboleth,一些自定义验证数据库,任何)集成。为了使插件兼容,它必须实现以下方法:

Interfaces are extremely useful when you need to define something like a plugin architecture. Suppose your application accepts authentication plugins, allowing your end user implementers to integrate with their own internal auth infrastructures (LDAP, Shibboleth, some custom auth database, whatever). In order for a plugin to be compatible, it must implement the following methods:

validate_user()
start_user_session()
logout_user()
get_user_details()

如果在接口中定义,然后实现接口,确保必要的方法是为了与插件架构兼容。

If defined in an interface, a class then implements the interface ensuring that the necessary methods are present for compatibility with the plugin architecture.

不幸的是,PHP不强制返回类型的接口方法,所以你必须只需注意记录方法的预期返回功能。

Unfortunately, PHP does not enforce the return type of interface methods, so you must simply take care to document the expected return functionality of your methods.

这篇关于PHP接口:它们如何在实践中可用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆