开发者

PHP: Passing Interface as Parameter

In .NET I have done that I passed Interfaces as parameters in class methods. I want to know is it possible in PHP?

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()
}

And then I do:

class MyClass
{
   public setNotifier(INoti开发者_运维百科fication $notifier)
  {
  }
}

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.

Thanks


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


Yes, you can do as you coded. You can find more information and examples on php.net.

Note that specifying the type in the method parameter (type hinting) is allowed (PHP >= 5), but not required.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜