Home » Php » PHP: Set private, protected, public in interface?

PHP: Set private, protected, public in interface?

Posted by: admin July 12, 2020 Leave a comment

Questions:

when I look through GitHub most projects define methods in the interface this way:

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?

How to&Answers:

what is the point of a private function in an interface? declaring public is redundant.

from 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

Answer:

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.

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.

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.

Answer:

Interfaces can only contain public methods, so the public is a bit redundant.

Answer:

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..

Answer:

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. Because it works the same when you read classes.

@AlphaMale Ok so what is the point of an interface then? 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. If a new developer works on a completely new process and looks at the interface then the send function will never be forced. 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.

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.