Class being redeclared after include - php

I have the following include code on a page:
include "cc/ConstantContact.php";
This file exists and contains the lines:
require_once('Authentication.php');
require_once('Collections.php');
require_once('Components.php');
When I load the file that includes constantContact.php, I get the following error:
Fatal error: Cannot redeclare class OAuthException in /public_html/cc/Authentication.php on line 13
I do not understand how this class is being declared twice, as there is only the one time that the constantContact.php file includes Authentication.php.
These includes work just fine for me on other sites as well.

Related

PHP - Fatal error: Cannot redeclare class Config in /path/to/Config.php on line 44

This is a WordPress local installation that I am trying to work with. I have not written a single line of this code myself. I don't understand what this error means:
Fatal error: Cannot redeclare class Config in /Applications/XAMPP/xamppfiles/lib/php/Config.php on line 44
Line 44 reads as follows:
class Config {
My guess is that a Config class has either already been declared elsewhere, or that this file is being executed for the second time.
That usually happens when you declare a class more than once in a page -- maybe via multiple includes.
To avoid this, use require_once instead. If you use require_once PHP will check if the file has already been included, and if so, not include (require) it again.
Say, for example, you have the following code:
<?php
class foo {
# code
}
... more code ...
class foo { // trying to re-declare
#code
}
In this case, PHP will throw a fatal error similar to the one below:
Fatal error: Cannot redeclare class foo in /path/to/script.php on line 7
In this case it's very simple -- simply find the 7th line of your code and remove the class declaration from there.
Alternativey, to make sure you don't try to re-declare classes, you can use the handy class_exists() function:
if(!class_exists('foo')) {
class foo {
# code
}
}
The best approach, of course, would be to organize all the configurations in one single file called config.php and then require_once it everywhere. That way, you can be sure that it will be included only once.
As for debugging the error, you could use debug_print_backtrace().
It's possible that the theme you are using refers to a file called config.php. If so use the following steps.
Try to find the config.php file and change it's name to configuration.php.
Find the files where they use config.php in the code and change it to configuration.php.

PHP Fatal error: Cannot redeclare class AdminImportControllerCore in

I have a script with cURL to initiate other script that import products and categories in prestashop.
This is the script which start with cURL:
define('_PS_ADMIN_DIR_', getcwd());
include_once(_PS_ADMIN_DIR_.'/../config/config.inc.php');
include_once(_PS_ADMIN_DIR_.'/../config/defines.inc.php');
include_once(_PS_ADMIN_DIR_.'/functions.php');
include_once dirname(__FILE__).'/../controllers/admin/AdminImportController.php';
if (!isset($_GET['entity'])) die();
$import = New AdminImportController();
switch ($_GET['entity']) {
case 0:
loadCategoriesPost();
$import->categoryImport();
break;
case 1:
loadProductsPost();
$import->productImport();
break;
}
My problem is that the seconds script generate an error from "include_once dirname(FILE).'/../controllers/admin/AdminImportController.php';":
PHP Fatal error: Cannot redeclare class AdminImportControllerCore in...
I have tried to use include_once, DIR, also I looked for in that included files a line with "new AdminImportController();" but I don't found nothing.
Thanks!
Look inside the AdminImportController.php file. My guess is that it is also including the file that defines the AdminImportControllerCore class, and that it is not using the include_once verity of the include functions.
That would mean that your main page is defining the class through the include_once call, and then when it includes the AdminImportController class, the include there kicks in and tries to re-define the core class, with the results you described.
I suggest you look into PHP's autoloader feature. It'll save you all the headaches associated with manually including files like that. It also tends to promote good file and class naming practices.

PHP Class Not Loading

I'm using WordPress and I'm trying to write a plugin that uses a file from another plugin. The URL to get the file is correct. I'm able to include the file when I'm calling a static function from the class, it is saying that the class is not loading.
//loading the file
$url=plugins_url().'/nextgen-gallery/admin/functions.php';
include $url;
The filename is functions.php and in it is a class defined nggAdmin
However, when i call the function nggAdmin::create_gallery();, i get the following error:
Fatal error: Class 'nggAdmin' not found in /var/www/html/wordpress/wp-content/plugins/Product/addProductForm.php on line 27
plugins_url() gives you the url, e.g. http://example.com/wordpress/wp-plugins/, not the server path - http://codex.wordpress.org/Function_Reference/plugins_url
Use WP_PLUGIN_DIR instead - http://codex.wordpress.org/Determining_Plugin_and_Content_Directories

How to debug a class redefinition in PHP?

I'm getting a PHP Fatal error: Cannot redeclare class Foo in /directory/ on line 20 error, but I have no idea where it's coming from. I'm always using require_once for this class file, and I'm not sure how to debug it. Can I get some kind of inclusion stack trace somehow? I'm running PHP 5, so case sensitivity such as descriped here should not be a problem: http://www.php.net/manual/en/function.include-once.php.
Use debug_backtrace in file where is class declared, but before it's declaration
Another approach is to rename the class that was redefined (also rename the file containing the class) and then fix all class-not-found errors you get from that. That should lead you to the code that is causing the redefinition. In my case it was a class_alias statement that was causing the problem.

Fatal error: Cannot redeclare class Customer

I'm getting this error:
Fatal error: Cannot redeclare class Customer
Then I added the following code:
if (!class_exists('Customer')) {
include('include/customer.class.php');
}
Why do I still get that error?
I have a file (file1.php) which has the Customer() class declared.
In file1.php I make an ajax call to file2.php
In file2.php I declare the Customer() class again.
In file2.php there is only 1 declaration of Customer() class.
Check if your server runs opcode cacher like APC - that's the cause of an error. I've runned into it recently.
Clearly due to the fact I issue:
if (!class_exists('Customer')) {
The class doesn't exist so the class itself is somehow duplicating itself.
I use this class in numerous other pages in the application without a problem.
I simply removed the whole thing:
if (!class_exists('Customer')) {
include('include/customer.class.php');
}
And it somehow worked which is preplexing!
If the class existed, the class file should never be included...
It doesn't exist therefore, the class is being included.
Once included, it says it's already included...
Very, very odd...
Well, it's working now... I guess i'll leave it be...
Use include_once(). If that still gives you an error, the problem is that you are declaring the class more than once in the file "include/customer.class.php"
http://php.net/include_once
The errors could be caused by a class defined multiple times, for example:
class Foo() {}
class Foo() {} // Fatal error
If you are not sure how many times your class will be included you can two things:
Use include_once() or require_once() in order to be sure that that file is required "once" only.
Write that code you provided every time you are including that file:
if (!class_exists('Customer')) {
include('include/customer.class.php');
}
I'd prefer the first though.
Your problem is the one described above. There must be a place where the class is declared multiple times. Without any code is hard to tell where.
Here's some references:
include_once()
require_once()
PHP: The Basics

Categories