Interface with the same class name

Asked

Viewed 201 times

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 it is not possible to define an interface with the same class name?
This is a bug or an interface is defined as a class?

I use PHP 7.0.1
I found this link that may be a bug, but I haven’t had a way to complete it:
Bug #51225 cannot define a class with the same name as an interface

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 whoever was 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 with the name Test, which of these two types the code below would be referring to?

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

A class can have hierarchy and methods not predicted in the interface it implements. The body of the above function could invoke in the parameter $test the methods of the class Test that were not declared in the interface Test?

Moreover, 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 different purposes should receive distinct names.

  • Type comes before name when defining interface and when using implements. That should be possible.

  • 1

    @Rowaraujo When declaring, yes, you say if it is a "class" or an "interface", and this would serve to differentiate. When using/consuming you do not tell if it is "class" or "interface", so there would be nothing else to differentiate beyond the name used in the declaration.

  • When using, the compiler knows how to differentiate from the word implements pq is only possible to implement interfaces! And that must be a bug yes... look at this link: https://bugs.php.net/bug.php?id=51225

  • 1

    @Rowaraujo I think the bug ticket you linked is more related to a function situation class_exists - she should or should not identify the interface? I don’t want to get into this PHP merit (I even program in PHP). I made an addendum about why, conceptually, these things really should have different names. And once again, the problem of not knowing what kind you’re talking about consumption and not in the statement - the word Implements is used in the class declaration and not for consumption.

  • I will search further and dps dps decide whether to close this question or n.

2

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

PHP interprets it that way, as if an interface were a class. Hence the error.

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

  • 1

    And also, it doesn’t make sense a class with the same name of an interface, are for different uses.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.