PHP:将接口作为参数传递 [英] PHP: Passing Interface as Parameter
问题描述
在.NET中,我已经完成了将接口作为参数传递给类方法的操作.我想知道PHP中有可能吗?
In .NET I have done that I passed Interfaces as parameters in class methods. I want to know is it possible in PHP?
我的见解是我有一个处理mqin系统功能的类.现在,我想与它集成通知系统.我想将通知系统分开,因为它不是系统的主要部分,而且我可以在其他地方使用它.如果我具有以下结构:
My scnerio is that I have a class dealing with mqin system functionality. Now I want to integrate Notification system with it. I want to keep notification system separate since it is not the main part of the system plus I can use it somewhere else. If I have the following structure:
Interface INotification
{
public set()
public send()
}
然后我做
class MyClass
{
public setNotifier(INotification $notifier)
{
}
}
那么在类中实现set()和send()之后,是否可以在这里访问它们?我想知道此C#示例的工作原理他们设置接口类型的参数.
So Is it possible that I can access set() and send() here after implementing them in a class? I want to know how this C# Example work that they set parameters of an Interface type.
谢谢
推荐答案
是的,有可能,正如您所写的那样.此类接口的示例: http://api.nette.org/2.0/source-Http.IResponse.php.html#18 以及此类参数的示例:
Yes, it is possible, pretty much as you wrote. Example of such interface: http://api.nette.org/2.0/source-Http.IResponse.php.html#18 and example of such parameter: http://api.nette.org/2.0/source-Http.Context.php.html#32
这篇关于PHP:将接口作为参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!