Bug #51225 [Asn->Csd]: cannot define a class with the same name as an interface

From: Date: Sat, 16 Jan 2016 12:37:09 +0000
Subject: Bug #51225 [Asn->Csd]: cannot define a class with the same name as an interface
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-198710@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=51225&edit=1 ID: 51225 Updated by: danack@php.net Reported by: xiaodujinjin at gmail dot com Summary: cannot define a class with the same name as an interface -Status: Assigned +Status: Closed Type: Bug Package: Class/Object related Operating System: Windows XP MT15i PHP Version: master Assigned To: johannes Block user comment: N Private report: N New Comment: This was improved in PHP 7. For the code: interface foo {} class foo implements foo {} new foo(); The error message is now: Fatal error: Cannot declare class foo, because the name is already in use in /in/Nnh4T on line 6 Which I believe meets the criteria of "a clearer error message as a quick-fix," Previous Comments: ------------------------------------------------------------------------ [2012-05-20 02:40:44] xiaodujinjin at gmail dot com xiaodujinjin@gmail.com ------------------------------------------------------------------------ [2012-05-20 02:39:55] xiaodujinjin at gmail dot com xiaodujinjin@gmail.com ------------------------------------------------------------------------ [2012-03-20 21:05:19] LaKing at D250 dot hu The language-developers have two options. Either a clearer error message as a quick-fix, or allowing interfaces and classes with the same name, with the necessary underlying updates properly implemented in the language. That would be the elegant, and logical way. ------------------------------------------------------------------------ [2010-09-23 08:03:40] i at walkinraven dot name I think you the fact of class and interface sharing a same namespace should be clearly written into the manual! And the better way, the two should not share a same namespace at all. It waste me some much time to check my project. ------------------------------------------------------------------------ [2010-08-05 23:38:58] bobalicious dot bob at gmail dot com In my opinion, in order to be valid the code snippet should read: if (!class_exists('foobar') && !interface_exists('foobar') ) { class foobar{} } The error message on attempting to declare a class with the same name as an interface should respond: Cannot declare class as an interface exists with that name The reverse message should also be possible. It does not make sense to allow an interface and a class to have the same name (type hinting is a great example why not), and 'class_exists' should only refer to classes (the clue's in the name). The confusion is merely down to an inaccurate error message. Note: There's a reason it's not uncommon for people to prefix interfaces with an 'i' ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at https://bugs.php.net/bug.php?id=51225 -- Edit this bug report at https://bugs.php.net/bug.php?id=51225&edit=1

« previous php.bugs (#198710) next »