php 是否可以有一个具有私有/受保护方法的接口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1876516/
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 04:08:24  来源:igfitidea点击:

Is it possible to have an interface that has private / protected methods?

phpoopinterface

提问by teepusink

Is it possible in PHP 5 to have an interface that has private / protected methods?

在 PHP 5 中是否有可能有一个具有私有/受保护方法的接口?

Right now I have:

现在我有:

interface iService
{
    private method1();
}

That throws an error:

这会引发错误:

Parse error: syntax error, unexpected T_STRING, expecting T_VARIABLE

解析错误:语法错误,意外的 T_STRING,期待 T_VARIABLE

I just want to have confirmation that it is the case that an interface can only contain public methods.

我只想确认接口只能包含公共方法的情况。

回答by Pascal MARTIN

The PHP manual page about interfacesexplicitly states:

关于接口PHP 手册页明确指出:

All methods declared in an interface must be public; this is the nature of an interface.

接口中声明的所有方法都必须是公共的;这是接口的本质。

I guess this explains the error you are getting ;-)

我想这解释了您遇到的错误;-)

回答by ryanday

Interfaces are used to describe public methods of a class implementing that interface. You can never have a private method in an interface. Any methods in an interface are assumed to be in use and should not be changed.

接口用于描述实现该接口的类的公共方法。接口中永远不能有私有方法。假定接口中的任何方法都在使用中,不应更改。

Interfacesis the PHP link, but this is standard in OO programming.

Interfaces是 PHP 链接,但这是 OO 编程中的标准。

回答by Webleeuw

In general an interface can only have public members, because the only function of an interface is to be inherited.

一般来说,接口只能有公共成员,因为接口的唯一功能是被继承。

From PHPfreaks.com tutorial:

来自 PHPfreaks.com 教程:

PHP5 features interfaces. Not to be confused with interfaces in the more general sense, the interface keyword creates an entity that can be used to enforce a common interface upon classes without having to extend them like with abstract classes. Instead an interface is implemented.

Interfaces are different from abstract classes. For one, they're not actually classes. They don't define properties, and they don't define any behaviour. The methods declared in an interface must be declared in classes that implement it.

Because an interface in the more general sense is a definition of how an object interacts with other code, all methods must be declared public (see section on visibility in this chapter). Using abstract classes, an abstract method can have any visibility, but the extending classes must have their implementations use the same (or weaker) visibility. Implementing an interface adds the methods as abstract methods to the subject class, failure to implement it will result in an error like the following:

Fatal error: Class SomeConcreteClass contains n abstract method(s) and must therefore be declared abstract or implement the remaining methodsYes, abstract classes can implement interfaces.

PHP5 具有接口功能。不要与更一般意义上的接口混淆,interface 关键字创建了一个实体,该实体可用于在类上强制实施公共接口,而不必像抽象类那样扩展它们。而是实现了一个接口。

接口不同于抽象类。一方面,它们实际上不是类。它们不定义属性,也不定义任何行为。在接口中声明的方法必须在实现它的类中声明。

因为更一般意义上的接口是一个对象如何与其他代码交互的定义,所以所有方法都必须声明为 public(参见本章中的可见性部分)。使用抽象类,抽象方法可以具有任何可见性,但扩展类的实现必须使用相同(或更弱)的可见性。实现接口将方法作为抽象方法添加到主题类中,如果不实现会导致如下错误:

致命错误:类 SomeConcreteClass 包含 n 个抽象方法,因此必须声明为抽象或实现其余方法是的,抽象类可以实现接口。

回答by just somebody

interfaces are type declarations. a type is set of values, plus a set of operations that can be carried upon them from outside. a private method doesn't fit into this picture.

接口是类型声明。类型是一组值,加上一组可以从外部对其进行的操作。私有方法不适合这张图片。

interface T {
  public /*int*/ function f(array $a);
}
interface U {
  public /*T*/ function g(T $t);
}

class C implements U {
    public function g(T $t) {
        ...
        $x = $t->f();
        ...
    }
}

interfaces are useful because they state, well, objects' interfaces. how the objects communicate with their environment.

接口很有用,因为它们说明了对象的接口。对象如何与其环境通信。

now let's say T::fcould be declared private. how would that be useful to other objects? it would not callable from outside, it would not be part of its interface.

现在让我们说T::f可以声明为私有。这对其他对象有什么用?它不会从外部调用,它不会成为其接口的一部分。

回答by Cristian Martinez

In many cases, an interface definition helps other modules guarantee the behavior and the API of a class. In those cases, private methods are not something the other modules can access or understand. That's why you can never put private methods on an interface.

在许多情况下,接口定义帮助其他模块保证类的行为和 API。在这些情况下,私有方法不是其他模块可以访问或理解的。这就是为什么你永远不能在接口上放置私有方法的原因。