Edit report at http://bugs.php.net/bug.php?id=51225&edit=1

 ID:                 51225
 Comment by:         bobalicious dot bob at gmail dot com
 Reported by:        tony at marston-home dot demon dot co dot uk
 Summary:            cannot define a class with the same name as an
                     interface
 Status:             Assigned
 Type:               Bug
 Package:            Class/Object related
 Operating System:   Windows XP
 PHP Version:        5.2.13
 Assigned To:        johannes
 Block user comment: N

 New Comment:

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'


Previous Comments:
------------------------------------------------------------------------
[2010-03-08 10:35:06] tony at marston-home dot demon dot co dot uk

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.

------------------------------------------------------------------------
[2010-03-08 00:55:52] johan...@php.net

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.

------------------------------------------------------------------------
[2010-03-08 00:28:39] der...@php.net

Yes, I agree.

------------------------------------------------------------------------
[2010-03-08 00:23:50] tony at marston-home dot demon dot co dot uk

If an interface is a class, then it should show up in class_exists() and
get_declared_classes().

------------------------------------------------------------------------
[2010-03-08 00:02:25] ka...@php.net

Thats how the OO is designed, internally is interfaces just a class with
an additional flag.



So no bug here

------------------------------------------------------------------------


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

    http://bugs.php.net/bug.php?id=51225


-- 
Edit this bug report at http://bugs.php.net/bug.php?id=51225&edit=1

Reply via email to