1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Fatal error: Cannot redeclare class

Discussion in 'XenForo Development Discussions' started by Chris D, Sep 14, 2012.

  1. Chris D

    Chris D XenForo Developer Staff Member

    OK.

    So I've got two add-ons that both use the Facebook API.

    My Friend Inviter and a yet to be released Social Status Updater which allows members to link their Facebook and Twitter accounts to their forum accounts so that when they update their status on XenForo it is automatically updated on Facebook, Twitter or both.

    Independently both add-ons work great. When both installed, I get this when going to the Friend Inviter page:

    Fatal error: Cannot redeclare class Facebook in C:\wamp\www\xenforo\library\SocialStatusUpdater\facebook\src\facebook.php on line 93

    This is my PHP code for Friend Inviter:

    PHP:
     
    try
    {
    require_once 
    "library/FriendInviter/facebook/src/facebook.php";
    }
    catch(
    Exception $e)
    {
    error_log($e);
    }

    and my PHP for Social Status Updater:

    PHP:
     
    try
    {
    require_once 
    "library/SocialStatusUpdater/facebook/src/facebook.php";
    }
    catch(
    Exception $e)
    {
    error_log($e);
    }

    I was under the impression that the try / catch would prevent the fatal error from occurring, but it doesn't.

    Have I missed something?

    I know that if both add-ons require the same paths (e.g. both load facebook.php from the FriendInviter folder) then the exception is caught and both add-ons remain functioning.

    Therefore, unless there's another solution I will have to start maybe using some sort of shared framework for my add-ons and packaging them under library/deeming/<addon> and that will enable me to have shared resources between certain releases.
     
  2. Sadik B

    Sadik B Well-Known Member

    What is in facebook.php? I am assuming a class declaration. Is it a class written by you? If yes, why do both the classes have to have same name? IF it's not a class written by you but a third party API, you should put the API under it's own folder inside Library. No point having the same source code in two different files. The error is exactly telling you what is wrong. The same class cannot be defined in two different places.
     
    Chris D likes this.
  3. Chris D

    Chris D XenForo Developer Staff Member

    Yeah it's a 3rd party API.

    I understand the error and what it's telling me, but I guess that answers my question.

    Cheers
     
  4. Chris D

    Chris D XenForo Developer Staff Member

    I got contacted by PM and someone suggested this, which works:

    Code:
    if(!class_exists('Facebook'))
    {
    
    require_once "library/FriendInviter/facebook/src/facebook.php";​
    }
    Thank you :)
     
    Jake Bunce, Adam Howard and Sadik B like this.

Share This Page