dataskills
dataskills

Reputation: 656

What is causing the error "Cannot declare class className because the name is already in use"

I cannot find the cause of this error.

I have a model (school profile) that has a relation to another model (fields of study).

When I try to access the related model I get the error "Cannot declare class app\models\FieldOfStudy because the name is already in use"

I am not aware of using it anywhere else.

Relation code:

public function getFieldsOfStudy()
{
    return $this->hasMany(FieldOfStudy::className(), ['fieldOfStudyId' => 'fieldOfStudyId'])
        ->viaTable('schoolProfileFieldOfStudyXref', ['schoolProfileId' => 'schoolProfileId']);
}

I am trying to access the related model like so:

$schoolProfile->fieldsOfStudy;

The thing that is particularly frustrating is that I use these same classes in another project. I have never seen this error. The error output indicates that the error is happening in the above hasMany function when it simply tries to load the class.

Any ideas?

Upvotes: 1

Views: 5934

Answers (1)

dataskills
dataskills

Reputation: 656

I had a typo in the namespace declaration in the FieldOfStudy class. I got the idea to check it from this post: symfony2 fatal error Cannot redeclare class

The exact part of the post that helped out was:

"Here is a hint: check if you have accidentally deleted or typo:ed the namespace in the file that contains the definition of the class that php claims it is trying to re-define."

Upvotes: 2

Related Questions