• 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

Chris D

XenForo developer
Staff member
#1
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.
 

Sadik B

Well-known member
#2
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

XenForo developer
Staff member
#3
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