• We just launched and are currently in beta. Join us as we build and grow the community.

How to Fix the "Fatal Error: Cannot Redeclare class" PHP Error

saadhfathi

User Acquisition Specialist
S Rep
0
0
0
Rep
0
S Vouches
0
0
0
Vouches
0
Posts
189
Likes
129
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
php-cannot-decalre-class-banner.jpg


If you are facing a PHP Error that says "Fatal Error: Cannot Redeclare class" or "Fatal Error: Cannot declare class ..., because the name is already in use ...", this article will help you fix or solve that and give you knowledge on how you can prevent this PHP error to occur in the future.

The "Fatal Error: Cannot Redeclare class" or "Fatal Error: Cannot declare class ..., because the name is already in use ..." is a PHP Error that occurs if you unintentionally redeclare the already loaded class name in your current PHP script. In other words, the class name that you entered for your new class is a duplicate or already exists.

Example: Class Already has been loaded multiple times

Building an application or website is not easy for the developers in order to provide an easy-to-use, efficient, and effective application to their end-users sometimes we forgot that we may already declare or included the class file script into another PHP script due to a large number of the file script line. Sometimes, we forgot that the class file script is already included in the file that we did include in the current PHP script. Check out the sample script below to understand more about the scenario.

index.php

  1. <?php
  2. include

    (

    'config.php'

    )

    ;
  3. include

    (

    'loadClasses.php'

    )

    ;
  4. ?>

config.php

  1. <?php
  2. include

    (

    'sampleclass.php'

    )

    ;
  3. ?>

loadClasses.php

  1. <?php
  2. include

    (

    'sampleclass.php'

    )

    ;
  3. ?>

sampleclass.php

  1. <?php
  2. class

    SampleClass{
  3. function

    __construct(

    )

    {
  4. echo

    '<h1>Hello World!!!</h1>'

    ;
  5. echo

    '<hr>'

    ;
  6. echo

    '<h1>You have loaded the Sample Class successfully</h1>'

    ;
  7. }
  8. }
  9. ?>

Result

As you can see, the provided PHP scripts result in a Fatal Error that says "Cannot declare class sampleclass, because the name is already in use". The error occurred even though the class name was written once in a file because the index.php file load 2 different PHP scripts that loads the sample class file script.

This Fatal Error can be fixed or solved in 2 ways which are by using the class_exist() function or include_once() statement. The class_exist() function checks the specific class name if it is already loaded and you can use it as the condition for the if statement to load the file that loads the class if not existing yet. The include_once() statement ignores the file to include if the file has already been included. Check out the solutions below.

Solution #1: Using class_exist

function


Here's an example script that fixes the error that occurred on the first PHP scripts I provided.

index.php

  1. <?php
  2. if

    (

    !

    class_exists

    (

    'SampleClass'

    )

    )
  3. include

    (

    'config.php'

    )

    ;
  4. if

    (

    !

    class_exists

    (

    'SampleClass'

    )

    )
  5. include

    (

    'loadClasses.php'

    )

    ;
  6. $sampleClass

    =

    new

    SampleClass(

    )

    ;
  7. ?>

Or we can also add the said function in each file script that is being loaded in the index.php.

config.php

  1. <?php
  2. if

    (

    !

    class_exists

    (

    'SampleClass'

    )

    )
  3. include

    (

    'sampleclass.php'

    )

    ;
  4. ?>

loadClasses.php

  1. <?php
  2. if

    (

    !

    class_exists

    (

    'SampleClass'

    )

    )
  3. include

    (

    'sampleclass.php'

    )

    ;
  4. ?>

Solution #2: Using include_once



We can also use the include_once to fix this PHP error. Note if you are using the require statement, you can use the require_once statement so that difference between the include and require statements will remain.

config.php

  1. <?php
  2. include_once

    (

    'sampleclass.php'

    )

    ;
  3. ?>

loadClasses.php

  1. <?php
  2. include_once

    (

    'sampleclass.php'

    )

    ;
  3. ?>

Both solutions fix or solve the "Fatal Error: Cannot Redeclare class" or "Fatal Error: Cannot declare class ..., because the name is already in use ...". You can always use the given solution for your future PHP Projects to avoid this kind of PHP error to occur even you unintentionally included the files with the same class file script multiple times.

Solution Script Result

There you go! That is how you can fix and prevent the "Fatal Error: Cannot Redeclare class" or "Fatal Error: Cannot declare class ..., because the name is already in use ..." PHP error. I hope this article will help you with what you are looking for and that you'll find this useful for your current and future PHP Projects. Feel free to leave a comment with any queries or questions regarding this article.

Explore more on this website for more Tutorials and Free Source Codes.

Happy Coding =)

 

440,010

316,559

316,568

Top