Site icon Nabtron

Fatal error: Cannot redeclare class FacebookApiException – Solution

This solution should work for any environment, but in my particular case it was like this:

Scenario:

I was coding a plugin for wordpress which uses the FacebookApiException (in facebook.php file or facebook app sdk). I included the file using:

include(facebook.php);

and boom! it gave me this Error:

Fatal error: Cannot redeclare class FacebookApiException in/Applications/XAMPP/xamppfiles/htdocs/1/wp-content/plugins/fb-frame/facebook.phpon line 107

Well don’t panic if you received similar error! It’s pretty simple to fix!

Solution:

To fix this Fatal error, replace:

	include(facebook.php);

With this code:

if (!class_exists('FacebookApiException')) {
	include_once('facebook.php');
}

Here we simply checked if the class FacebookApiExecution exists already or not. If not then include this file, otherwise don’t!

Note: you might be wondering why was class existing already? well maybe you have another plugin activated in your wordpress installation which calls this file and function too! which caused it to give error that can’t redefine the class!

You can use it on other platforms like drupal and joomla! too, or any custom coded php page too!

Exit mobile version