简体   繁体   中英

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.

  1. Try to find the config.php file and change it's name to configuration.php.
  2. Find the files where they use config.php in the code and change it to configuration.php.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM