PHP这类弱类型语言的接口有什么意义? [英] What is the point of interfaces in a weakly-typed language like PHP?

查看:83
本文介绍了PHP这类弱类型语言的接口有什么意义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从来没有弄明白这一点。如果您的语言没有进行类型检查,那么接口会为您带来哪些好处?

I've never been able to figure this out. If your language doesn't type-check, what benefits do interfaces provide you?

推荐答案

接口会导致您的程序提前失败,当子类忘记在其父类中实现一些抽象方法时,更可预测。

Interfaces cause your program to fail earlier and more predictably when a subclass "forgets" to implement some abstract method in its parent class.

在PHP的传统OOP中,您必须依赖以下内容来发出运行时错误:

In PHP's traditional OOP, you had to rely on something like the following to issue a run-time error:

class Base_interface {
    function implement_me() { assert(false); }
}

class Child extends Base_interface {
}

使用接口,当你的某个接口的子类没有实现这样的方法时,在声明子类时,而不是在使用它时,你会得到即时反馈。

With an interface, you get immediate feedback when one of your interface's subclasses doesn't implement such a method, at the time the subclass is declared rather than later during its use.

这篇关于PHP这类弱类型语言的接口有什么意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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