php 如何创建由其他接口组成的接口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13942727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:18:56  来源:igfitidea点击:

How to create an interface comprised of other interfaces?

phpinterface

提问by FtDRbwLXw6

I'd like to create an interface, IFoo, that's basically a combination of a custom interface, IBar, and a few native interfaces, ArrayAccess, IteratorAggregate, and Serializable. PHP doesn't seem to allow interfaces that implement other interfaces, as I get the following error when I try:

我想创建一个接口,IFoo即基本上是一个自定义接口的组合,IBar和一些原生接口,ArrayAccessIteratorAggregate,和Serializable。PHP 似乎不允许实现其他接口的接口,因为我在尝试时收到以下错误:

PHP Parse error: syntax error, unexpected T_IMPLEMENTS, expecting '{' in X on line Y

PHP解析错误:语法错误,意外的T_IMPLEMENTS,在Y行的X中期望'{'

I know that interfaces can extend other ones, but PHP doesn't allow multiple inheritance and I can't modify native interfaces, so now I'm stuck.

我知道接口可以扩展其他接口,但是PHP不允许多重继承,我不能修改本机接口,所以现在我卡住了。

Do I have to duplicate the other interfaces within IFoo, or is there a better way that allows me to reuse the native ones?

我是否必须在 中复制其他接口IFoo,还是有更好的方法可以让我重用本机接口?

回答by hakre

You are looking for the extendskeyword:

您正在寻找extends关键字:

Interface IFoo extends IBar, ArrayAccess, IteratorAggregate, Serializable
{
    ...
}

See Object Interfacesand in specific Example #2 Extendable Interfaces ff.

请参阅对象接口和特定示例 #2 可扩展接口 ff

回答by furkanali89

You need to use the extendskeyword to extend your interface and when you need to implement the interface in your class then you need to use the implementskeyword to implement it.

你需要使用extends关键字来扩展你的接口,当你需要在你的类中实现接口时,你需要使用implements关键字来实现它。

You can use implementsover multiple interfaces in you class. If you implement the interface then you need to define the body of all functions, like this...

您可以implements在类中使用多个接口。如果你实现了接口,那么你需要定义所有函数的主体,就像这样......

interface FirstInterface
{
    function firstInterfaceMethod1();
    function firstInterfaceMethod2();
}
interface SecondInterface
{
    function SecondInterfaceMethod1();
    function SecondInterfaceMethod2();
}
interface PerantInterface extends FirstInterface, SecondInterface
{
    function perantInterfaceMethod1();
    function perantInterfaceMethod2();
}


class Home implements PerantInterface
{
    function firstInterfaceMethod1()
    {
        echo "firstInterfaceMethod1 implement";
    }

    function firstInterfaceMethod2()
    {
        echo "firstInterfaceMethod2 implement";
    }
    function SecondInterfaceMethod1()
    {
        echo "SecondInterfaceMethod1 implement";
    }
    function SecondInterfaceMethod2()
    {
        echo "SecondInterfaceMethod2 implement";
    }
    function perantInterfaceMethod1()
    {
        echo "perantInterfaceMethod1 implement";
    }
    function perantInterfaceMethod2()
    {
        echo "perantInterfaceMethod2 implement";
    }
}

$obj = new Home();
$obj->firstInterfaceMethod1();

and so on... call methods

等等......调用方法