Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Set private, protected, public in interface?

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?

like image 625
bodokaiser Avatar asked May 28 '12 11:05

bodokaiser


People also ask

CAN interface have private methods PHP?

All methods declared in an interface must be public; this is the nature of an interface. In practice, interfaces serve two complementary purposes: To allow developers to create objects of different classes that may be used interchangeably because they implement the same interface or interfaces.

Can we use protected method in interface?

Protected members of an interface In general, the protected members can be accessed in the same class or, the class inheriting it. But, we do not inherit an interface we will implement it. Therefore, the members of an interface cannot be protected.

CAN interface have constants in PHP?

Interfaces can contain methods and/or constants, but no attributes.

Can a method inherited from an interface have a private access modifier?

First of all Interfaces are used to specify common methods for a set of unrelated classes for which every class will have a unique implementation. Therefore it is not possible to specify the access modifier as private since it cannot be accessed by other classes to be overridden.


1 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

like image 168
Not_a_Golfer Avatar answered Sep 22 '22 12:09

Not_a_Golfer