|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2010-03-06 18:50 UTC] xiaodujinjin at gmail dot com
Description:
------------
When I try to define a particular class it fails with "cannot redeclare class ...". When I check with class_exists('...') it returns false, but I still cannot create it. I eventually found some previous code which uses the same name to define an interface.
Test script:
---------------
Interface Singleton{public static function instance();}
if (class_exists('Singleton')) {
$reason = 'class already exists';
} else {
class Singleton{
static function getInstance(){
return true;
}
}
}
Expected result:
----------------
If it is not possible to define a class and an interface with the same name, then the class_exists() function should also include interface names.
If it IS possible to have a class and an interface with the same name, then the compiler should NOT reject the second reference.
Patches1314609a (last revision 2012-05-20 02:45 UTC by xiaodujinjin at gmail dot com)Pull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Dec 01 15:00:01 2025 UTC |
I think the error message ("Cannot redeclare class") should be clearer about classes and interfaces sharing the same namespace, which is needed as type hints would be conflicting otherwise, but class_exists (by default) should only check classes in my opinion. Any change should consider that there's also interface_exists() and they should be consistent.I disagree. class_exists() SHOULD check if that name has already been declared as an interface otherwise you get the following situation: if (!class_exists('foobar') { // returns false class foobar{} // fails because interface exists } On the one hand it is saying "a class with the name 'foobar' does not exist" which is immediately followed by "you cannot create a class with the name 'foobar' as it already exists". That is not logical to me.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'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,"