I've got 2 problems here.

I have an entity that references itself in field definition (see below).

Problem No.1. I've got a fatal error PHP Fatal error: Cannot redeclare class Drupal\mymodule\MyEntity when trying to install the module.

Problem No.2 I now get it every time even when I remove the referencing field and do drush cr unless I wrap the class definition in !class_exists().

I swear this is the only file defining the class.

Is there a way to clean this out of wherever drupal has stored it?

And how do I declare this type of self-reference?


namespace Drupal\mymodule;

use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;

 * @ContentEntityType(
 *     id = "myentity",
 *     label = "My Entity",
 *     base_table = "myentity"
 * )
class MyEntity extends ContentEntityBase
    public static function baseFieldDefinitions(EntityTypeInterface $entity_type)
        $fields = [];

        // Standard field, used as unique if primary index.
        $fields['id'] = BaseFieldDefinition::create('integer')

        $fields['myentity_id'] = BaseFieldDefinition::create('entity_reference')

        $fields['name'] = BaseFieldDefinition::create('string');

        return $fields;
  • It's funny, but even renaming entity and class doesn't help. It will still say PHP Fatal error: Cannot redeclare class Drupal\mymodule\MyEntityz
    – Okneloper
    Oct 20, 2016 at 8:45

1 Answer 1


Ok so the problem was in the namespace:

namespace Drupal\mymodule;

From my observation (can't find this in the Entity API documentation entities are supposed to be placed in the src/Entity (which I did) which imposes the Entity namespace. So fixing the namespace solved my issue.

namespace Drupal\mymodule\Entity;

What a stupid way to spend a half of a day. Looks like I'm too used to class loaders that will simply not find the class if you place it in the wrong directory.

  • Drupal's class loader does not find it if you put it in the wrong directory. But that's not what you did. You put it in the right directory, with the wrong namespace. Which means that the class loader finds the file, includes it, but the expected class still doesn't exist yet. Then there is another attempt at loading it and it fails because the file class defined it in already exists. No class loader can prevent you from that.
    – Berdir
    Oct 23, 2016 at 11:22
  • 1. Right directory for the Drupal\mymodule\MyEntity class is {module_dir}/ and {module_dir}/Entity/ is wrong directory. So loading Drupal\mymodule\MyEntity from {module_dir}/Entity/MyEntity.php is also wrong and should not happen, let alone twice...
    – Okneloper
    Oct 23, 2016 at 14:00
  • Entity types are found based on annotation discovery, which is file based, then the class name is derived from that. So yes, if Drupal would look for Drupal\mymodule\MyEntity, it would simplfy fail and not find it, but it doesn't do that, it looks for Drupal\mymodule\Entity\MyEntity, finds your file for it but the class name there is not as expected. The only way to detect that would be to do a class exists check after loading the file, Drupal uses standard composer class loading, so you'd have to open an issue there to do that.
    – Berdir
    Oct 23, 2016 at 15:19
  • Sorry, why would I open an issue with composer for something that "Drupal doesn't do that"? When using composer autoloader the right way there is no such problem - you reference a class, composer loads the file, and if there was wrong class in the file PHP fails with 'Unknown class'.
    – Okneloper
    Oct 24, 2016 at 8:46
  • I don't know why Drupal does it the way it does, but that's a major problem with Drupal - you get either no error message whatsoever or an error message that's completely useless, like this The 'set' plugin does not exist. Obviously this does not give any clue to what type of plugin it's talking about, where is it supposed to be placed, what class name, nothing that can help the developer resolve it.
    – Okneloper
    Oct 24, 2016 at 8:51

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.