简体   繁体   English

PHP:在接口中设置私有的,受保护的,公共的吗?

[英]PHP: Set private, protected, public in interface?

when I look through GitHub most projects define methods in the interface this way: 当我浏览GitHub时,大多数项目都以这种方式在接口中定义方法:

interface ExampleInterface
{
    function getId();
}

my question now is why it is bad style to define the method visability in the Interface: 我现在的问题是为什么在接口中定义方法可见性是一种不好的风格:

interface ExampleInterface
{
    public function getId();
}

It makes the interface more strict but isn't that whats an interface used for? 它使接口更加严格,但这不是接口用于做什么?

what is the point of a private function in an interface? 接口中私有功能的意义是什么? declaring public is redundant. 宣布public是多余的。

from TFM: 来自TFM:

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

http://php.net/manual/en/language.oop5.interfaces.php http://php.net/manual/en/language.oop5.interfaces.php

It is because an interface is a promise you give to the outside world of certain functionality. 这是因为接口是您向外界提供某些功能的承诺。 In your above example, whenever a class implements an interface, it is guaranteeing that the class will provide a method called getId to the outside world, irrespective of how it is implemented. 在上面的示例中,无论何时类实现接口,都保证该类将向外界提供称为getId的方法,而不管其实现方式如何。

Hence, if you make a private promise , it is irrelevant as no one cares if there is a private method with some functionality, it is anyways not accessible by anyone else. 因此,如果您做出私有promise ,那么这是无关紧要的,因为没有人会在乎是否有一种具有某些功能的私有方法,无论如何其他人都无法访问它。

On the other hand, all methods in an interface are essentially public (since they are nothing but promises to the outside world) and hence you explicitly mentioning it as public is redundant. 另一方面,接口中的所有方法本质上都是公共的(因为它们不过是向外界的承诺而已),因此您明确地将其称为public是多余的。

接口只能包含公共方法,因此公共有点多余。

An interface can only have public members so there's no need to declare it . 接口只能具有公共成员,因此无需声明它 And these functions are meant to be inherited . 这些功能是要继承的 Therefore; 因此; All methods declared in an interface must be public, that is its nature .. 接口中声明的所有方法都必须是公共的,这就是它的本质 ..

I can understand that private cannot work but protected as well? 我能理解私有不能工作但也能受到保护吗?

I understand it is a "promise" to the outside world but if something implements an interface that method will stay protected therefore the same "promise" was still kept. 我知道这是对外界的“承诺”,但是如果某种方法实现了一个接口,该方法将受到保护,因此仍然保留了相同的“承诺”。 It stated that there is function a, function b and also protected function c so from that we derive if I am not extending/implementing this then I can not have access to function c. 它表示存在一个函数a,一个函数b和一个受保护的函数c,因此从中我们可以得出,如果我不扩展/实现该函数,那么我将无法访问函数c。 Because it works the same when you read classes. 因为当您阅读课程时它的工作原理相同。

@AlphaMale Ok so what is the point of an interface then? @AlphaMale好,那么接口的意义是什么? It is used to create a "recipe" for a class right? 它是用来为班级创建“食谱”的吗? So when you use it practically in code it will only be to implement and force methods. 因此,当您在代码中实际使用它时,只会实现和强制执行方法。 This becomes handy if methods are needed for your code to work and you don't want to force it to a specific class. 如果您的代码需要使用方法,而您又不想将其强制到特定的类,这将变得很方便。

Lets say I have a to create Mailers, but I have to major processes where mails are needed but mailers will use different ways of constructing these mails. 可以说我有一个创建邮件程序的地方,但是我必须处理需要邮件的主要过程,但是邮件程序将使用不同的方式来构造这些邮件。 Now There needs to be a base send function that takes a message(any type) and a email address, but since the classes are in an open environment then it will be dangerous to leave this send function public and therefore we need it as a protected method. 现在需要有一个包含消息(任何类型)和电子邮件地址的基本发送函数,但是由于这些类处于开放环境中,因此将该发送函数公开是很危险的,因此我们需要将其作为受保护对象方法。 Now in the two Processes there are multiple mailers but each process shares the same way of constructing the method, so there is 2 abstract classes but now due to limited interfaces you now have to declare a third abstract class to contain this protected send function so that it can be extended as well otherwise you will have the same protected send function. 现在在两个进程中有多个邮件程序,但是每个进程共享相同的方法构造方法,因此有2个抽象类,但是由于接口有限,现在您必须声明第三个抽象类以包含此受保护的send函数,以便它也可以扩展,否则您将具有相同的受保护的发送功能。 If a new developer works on a completely new process and looks at the interface then the send function will never be forced. 如果新的开发人员正在使用全新的流程并查看界面,则永远不会强制执行send函数。 If we say it was possible to declare protected in an interface then outside if you check if something is an interface it will work the same as a class, you still can't access the the protected functions. 如果我们说可以在一个接口中声明protected,那么在外部如果您检查某物是否是一个与类相同的接口,则仍然无法访问受保护的函数。

Private makes sense because then only the interface will know about these values and since an interface can't do anything this will be pointless. 私有是有意义的,因为只有接口才能知道这些值,并且由于接口无法执行任何操作,因此这将毫无意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM