与不需要的方法接口 [英] Interface with not-required methods

查看:74
本文介绍了与不需要的方法接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以在接口中指示可选方法(以便合同仅指示要提供的参数的数量/类型)?

Is there a way to indicate optional method in the interface (so that the contract only indicated the number / type of arguments to be given)?

请提供更多有关问题的理解和见解,并指出解决方案?例如,请参见以下讨论: Java接口中的可选方法

Please give maybe a little more understanding and insight into the problem, and indicate a solution? See for instance this discussion: Optional Methods in Java Interface

在应用程序中,我正在使用连接到Persistence(Doctrine)的侦听器.所以我正在使用其中一些方法:

In the app I'm using Listeners connected to the Persistence (Doctrine). So I'm using some of these methods:

prePersist()
preUpdate()
postPersist()
postUpdate()

现在,在重构时,由于实体(要保留的对象)太多,我决定将这些方法的各个部分拆分为单独的类.

Now, while refactoring, since there are too many Entities (objects to be persisted) I decided to split the parts of these methods into separate classes.

但是,并非所有人都需要所有pre -...和post -...方法.我需要确保为他们提供了适当数量和类型的参数.您如何在PHP中做到这一点?

However not all of them need all pre-... and post-... methods. I need to make sure they are given appropriate number and type of arguments. How do you do that in PHP?

推荐答案

否.接口的整个想法是要有一个保证一种方法存在的契约.

No. The whole idea of interfaces is to have a contract that guarantees that a method exists.

但是一个类可以实现多个接口,因此您可以定义一个包含该方法的不同接口,而不必将该接口添加到没有该方法的类中.

But a class can implement multiple interfaces, so you can define a different interface that contains that method and not add that interface to the class that doesn't have the method.

这篇关于与不需要的方法接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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