Interface with the same name as a class

0
namespace path;

interface test{
    public function method();
}

class test{

}

class foo implements test{

}

When writing the above code a fatal error is returned:

Cannot declare class path\test, because the name is already in use in ...  on line 7

Why can not I define an interface with the same name as a class?
Is this a bug or is an interface defined as a class?
I use PHP 7.0.1
I found this link which may be a bug, but I have not yet figured it out:
Bug # 51225 can not define the class with the same name as an interface

    
asked by anonymous 11.01.2016 / 17:57

2 answers

4
  

An interface is a declaration of a type, and a class is also a declaration of a type. Types must have distinct names in context / scope .

Otherwise, during type consumption, the compiler / interpreter and also anyone reading the code would not know which type you are referring to.

See your example code - if you could declare both an interface and a class named Test , which of these two types would the code below be referring to?

function funcao(Test $test) {
    // ...
}

A class can have hierarchy and unpredictable methods in the interface that it implements. The body of the above function could invoke in the $ test interface the methods of the Test class that were not declared in the Test interface? In addition, conceptually speaking, a class and an interface have distinct purposes, and if the name of something should reveal or give a good indication of its purpose, it is natural that things with distinct purposes are given different names.

    
11.01.2016 / 18:04
2

In OOP, an interface is a class with a specific flag that identifies it as an interface type.

PHP interprets this way, as if an interface were a class. So the error.

An interface must have a name that does not match the name of an existing class.

    
11.01.2016 / 18:09