简体   繁体   中英

PHP: How to solve 'Cannot redeclare loadModule function'

I am working on a web project and I have a file called init.php which basically initializes database connection and loads all the classes using spl_autoload_register.Its included at the starting of the page.

The file works fine usually but the error occurs when m including this file in a sub directory page. Like this ...

include '../includes/init.php';

I am getting this fatal error: Cannot redeclare loadModue previously declared on line ....

The file content looks like this :

<?php
    ob_start();

    $profile_time = array() ;
    $profile_time['start_time'] = microtime(TRUE) ;

    include_once( 'settings.php' ) ;
    //ini_set('display_errors', 'on');
    //ini_set('error_reporting', E_ALL);
    // set correct timezone
    date_default_timezone_set('Asia/Calcutta');

    // Set default encoding for multibyte strings
    mb_language('uni');
    mb_internal_encoding('UTF-8');
    mb_http_input('UTF-8');
    mb_http_output('UTF-8');
    mb_regex_encoding('UTF-8');

    function loadModule($className)
    {
        if(file_exists(APP_ROOT.'modules/'.$className.'.php'))
            require_once(APP_ROOT.'modules/'.$className.'.php');
    }

    spl_autoload_register('loadModule');

    $profile_time['before_sql_connection_time'] = microtime(TRUE) ;

    $DB = mysqli_connect( $db_data['host'] , $db_data['user'] , $db_data['pass'] , $db_data['db']);
    Sql::init(array('db' => $DB));

    $profile_time['after_sql_connection_time'] = microtime(TRUE) ;

    @session_start();

    if(User::isLoggedIn())
        $_user = new User($_SESSION['user_id']);
    else
        $_user = new User(0);
    if(isSet($_SESSION['user_id']))
        $user_data = $_user->getDataArray();

    ob_end_clean();
?>

The settings.php defines HOST, APP_ROOT etc n DB data....

I tried using

if(!function_exists(loadModule)){
       function loadModule(){.....}
    }

but this is giving class Sql not found fatal error ... basically not loading the classes.

I tried changing the name of the function to loadModule_new but that is giving same error of cannot redeclare .

All the cases that I could find on StackOverflow were not working from starting but m getting this problem only in case of inclusion inside a subdirectory.

You are including the file multiple times, and chaos ensues.

The best solution for such problems is to have a crystal clear plan about which file is responsible for including which .

Don't just dump an include('whatever.php') every time you need to bring some code into scope. Take a step back and design your include strategy. If you do this correctly, you will never have such problems again.

Failing that, using include_once instead of include should help you to work around these problems.

function_exists expects quote marks around the parameter:

if(!function_exists('loadModule')){
       function loadModule(){.....}
    }

I think the main problem is that some other file you're including is probably including '../includes/init.php'

Use include_once ('../includes/init.php'); and see if it works.

Also, search all your code base for this app for function loadModule (with a variable number of spaces just in case) to make sure that you've not got a definition anywhere else for loadModule

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