PHP namespaces and using the \ prefix in declaration

Posted by Jason McCreary on Stack Overflow See other posts from Stack Overflow or by Jason McCreary
Published on 2012-10-26T15:02:02Z Indexed on 2012/10/26 17:01 UTC
Read the original article Hit count: 178

Filed under:
|

The following throws an error stating Exception can no be redeclared.

namespace \NYTD\ReadingListBackend;

class Exception extends \Exception
{
}

However, removing the \ prefix in the namespace declaration does not:

namespace NYTD\ReadingListBackend;

I recently adopted PHP namespaces. My understanding is that namespaces prefixed with \ are a fully qualified name.

So why can't I use the prefix in the namespace declaration? I can when referencing (e.g. new \NYTD\ReadingListBackend\Exception). Would appreciate a full explanation as I couldn't find anything in the docs.

© Stack Overflow or respective owner

Related posts about php

Related posts about namespaces