We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Loading a third party module from within a plugin - "Cannot redeclare class" - Caching issue?

Development and Programming

upshot's avatar
upshot
85 posts
14 years ago
upshot's avatar upshot

I have a very strange issue occurring, perhaps due to some type of caching. I’m attempting to build my first plugin and I need to load a third party module from within my plugin. In this case it’s the Channel Ratings module from DevDemon, but I’m not sure that fact matters. I load the module like so:

$this->EE->load->add_package_path(PATH_THIRD.'channel_ratings/');
require(PATH_THIRD.'channel_ratings/mod.channel_ratings.php');
$objChannelRatings = new Channel_ratings();

Then, to test that this is working properly, I make a simple call to a function, in which I’m returning a simple string:

return $objChannelRatings->testing();

I set the testing() function to return a test string, “AAA”, and this is then output to the screen. When I load my browser the first time I get an error:

Fatal error: Cannot redeclare class channel_ratings in /path/to/expressionengine/third_party/channel_ratings/mod.channel_ratings.php on line 13

But then when I refresh my browser again, it works just fine and the error is gone from any future reloads. The error only occurs the first time I load the page. That is, until I change the testing() function to return a different test string, like “BBB”. Every time I change the output of the function, the first page load with yield the error but then all page loads after that work just fine.

This behavior happens in Firefox on Mac. Chrome for Mac acts the same way but instead of seeing the error I am getting a 500 Internal Server Error instead.

Does this behavior make sense to anyone? I don’t think it has anything to do with the coding of the module itself but I suppose I could be wrong.

       
JohanD's avatar
JohanD
59 posts
14 years ago
JohanD's avatar JohanD

Fatal error: Cannot redeclare class means it has already been included. Not sure what you’re trying to achieve here, but a require_once() would fix that fatal error.

       
Manuel Payano's avatar
Manuel Payano
144 posts
14 years ago
Manuel Payano's avatar Manuel Payano

One thing we always do, is check if the class already has been included.

if (class_exist('Channel_ratings') == FALSE) require(PATH_THIRD.'channel_ratings/mod.channel_ratings.php');
       
upshot's avatar
upshot
85 posts
14 years ago
upshot's avatar upshot

Thanks, guys. Your suggestions did the trick.

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.