Been getting PHP Fatal error: Cannot redeclare class Facebook error since...

Can anyone please enlighten what does this means?

Recommended Answers

All 2 Replies

Cannot redeclare class Facebook

You cannot redeclare an existing class.

UPDATE! this will only work to check if the class exists, but will not be able to check if there is an instance of the class already been instantiated.. try looking into this PHP function called instanceof.

you can also create a simple conditional checkpoint.. this is important in real world practice, because as your application gets bigger, you will not be able to track all of the instances going on and off within your appplication.

one method is to use an autoload or to use any of the 5 design patterns e.g. factory,,singleton,observer,chain of command and strategy patterns.
second, is to check if the class exist before actually initializing it. For example, we can put this on top of the php document requiring a class file and needing an instance of that class..

if (class_exists('FaceBookAPI'){
include_once ('location_of_facebook_API_class');
$fb_class = new FaceBookAPI();
}

To learn more about this method, please read my reference.

Here are some of the php functions that can help and can save dosages of headache medication :). I lifted them here..

method_exists() - Checks if the class method exists

is_callable() - Verify that the contents of a variable can be called as a function

get_defined_functions() - Returns an array of all defined functions

class_exists() - Checks if the class has been defined

extension_loaded() - Find out whether an extension is loaded

You can also prevent this from happening to other classes in your application by creating an instance manager script..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.