0

I start getting below error after defining a second controller class PostController in different bundle of the same project with the same vendor name.

Fatal error: Cannot redeclare class Amce\Bundle\CrudzBundle\Controller\PostController in C:\xampp\htdocs\community\src\Amce\CrudzBundle\Controller\PostController.php on line 350

I understand that this error means that I have the same name for two classes (OOP). But why even if I have a different bundle with different vendor part, I keep having this error? Does it mean that Synfony2 disallows having two controller classes with the same name in all situations?

Your expert explanations are always appreciated.

3
  • AFAIK the symfony does nothing like that, and you can not have 2 classes (or interface or abstract or trait) with the same names in the same namespace. The vendor namespace and your namespace are the same? Commented Jan 14, 2016 at 9:00
  • @mloureiro, I hope I am not wrong about what vendor means, but here I have Amce as vendor and AmceCrudzBundle as namespace. am I wrong? thanks Commented Jan 14, 2016 at 9:02
  • @mloureiro, but I already had a controller with the same name in AmceFruitsBundle and then in ShopManagementBundle with the same error Commented Jan 14, 2016 at 9:04

1 Answer 1

3

I assume the namespace of the culprit class is:

namespace Amce\Bundle\CrudzBundle\Controller

However the file path is:

C:\xampp\htdocs\community\src\Amce\CrudzBundle\Controller\PostController.php 

If you copy/pasted the original class, you may have forgotten to change the namespace.

The autoloader would check the this directory for a class that doesn't exist (because of said namespace), however before that, it will have discovered the exact same namespace/class previously.

In PHP 5.3, the namespace is incorporated into the class name. It's important to remember there is no distinction between them, because they are combined at compile time.

Despite the fact you can call __NAMESPACE__ to get the current namespace, in reality this is not performing a dynamic introspection of the code, but instead the magic constant was converted to a constant string at compile time.

The same is true with classes, the namespace becomes part of the class name, and that is how the class is indexed in the internal reference table.

So be mindful of namespaces.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for pointing me that it may be a copy/paste problem, I resolved the problem by changing namespace Amce\Bundle\CrudzBundle\Controller; to namespace Amce\CrudzBundle\Controller; in the controller file and it is working now. Thank you very much
I don't fully understand (honneslty). In all my controllers of the project, I was using namespace Amce\Bundle\CrudzBundle\Controller; and now for this specific controller, I have to omit /Bundle from it? !! really weird
@whitelettersinblankpapers You likely altered your directory structure. Though what is puzzling to me is why that error says the class is on line 350.
Yes sir, this is it. I think I am tired and should go to sleep ! :-) Have a nice day

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.