Compile Error: Cannot declare class because the name is already in use

Compile Error: Cannot declare class because the name is already in use

  1. Blog
  2. 15 November 2026
  3. 2 min read

Stefan Bogdanescu

Founder & Senior Architect

Compile Error: Cannot declare class because the name is already in use

When migrating from Symfony 2.6 to 3.4, you might encounter a specific PHP compilation error: Cannot declare class Pix\UserBundle\Form\RegistrationFormType because the name is already in use. This isn't a configuration issue or a missing dependency; it is a fundamental violation of how PHP namespaces and autoloading work.

The source of this conflict lies in the directory structure provided. You have defined two separate files with the exact same class name, RegistrationFormType, residing in different directories under Pix\UserBundle\Form. One resides at src/Pix/UserBundle/Form/RegistrationFormType.php (the root level), while the other exists inside a subdirectory named Type (src/Pix/UserBundle/Form/Type/RegistrationFormType.php).

Even though these files live in different folders, PHP's autoloader treats them as distinct classes only if their fully qualified names differ. In this scenario, both files are attempting to declare a class with the namespace prefix Pix\UserBundle\Form\. When the application attempts to load the bundle or instantiate the form type, the autoloader loads one file. If you then attempt to use the same FQN or if the autoloading strategy is ambiguous, PHP throws the "already in use" error because it cannot declare a second class with that specific namespace and name combination.

The resolution requires restructuring the code so that each class has a unique, fully qualified path. The standard practice for Symfony forms is to keep the base form type at the root of the Form directory or within a specific subdirectory without ambiguity.

Resolving the Collision

You must ensure that one of these files is removed or renamed to create a clear hierarchy. If your intention is to extend the FOSUserBundle registration form, you should consolidate the logic into a single location.

Assuming the file in the Type subdirectory (Pix\UserBundle\Form\Type\RegistrationFormType) is the one intended to be used based on the service definition, you have two options:

  1. Rename the conflicting class: Move the content of src/Pix/UserBundle/Form/RegistrationFormType.php into a different namespace if it needs to remain separate (e.g., Pix\UserBundle\Form\LegacyRegistrationFormType).
  2. Delete or Merge: If both files contain similar logic intended for the same bundle, merge the configurations carefully and delete one file entirely.

Updating the Service Definition

Your services.yml currently points to the class in the Type subdirectory:

pix_userbundle_registrationformtype:
    class: Pix\UserBundle\Form\Type\RegistrationFormType
    arguments: ["%fos_user.model.user.class%"]
    tags:
        - { name: form.type }

This is correct provided that the file src/Pix/UserBundle/Form/Type/RegistrationFormType.php exists and does not conflict with a class defined at src/Pix/UserBundle/Form/RegistrationFormType.php. If you are using Symfony 3.4, you should also ensure your service definitions align with modern practices. While the legacy syntax shown works, consider updating to the newer tagged iterator or explicit definition style if you are refactoring other parts of the bundle:

# Recommended approach for Symfony 3.4+ services.yml
Pix\UserBundle\Form\Type\RegistrationFormType:
    parent: form.type
    arguments:
        - "%fos_user.model.user.class%"

Modernizing Form Construction in Symfony 3.4

The code snippets provided rely on older PHP interfaces like OptionsResolverInterface and

Our network