[英]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. 我正在一个Web项目上工作,我有一个名为init.php的文件,该文件基本上初始化数据库连接并使用spl_autoload_register加载所有类,该文件包含在页面的开头。
The file works fine usually but the error occurs when m including this file in a sub directory page. 该文件通常可以正常工作,但是当m在子目录页面中包含此文件时,会发生错误。 Like this ...
像这样 ...
include '../includes/init.php'; 包括'../includes/init.php';
I am getting this fatal error: Cannot redeclare loadModue previously declared on line .... 我收到此致命错误:无法重新声明先前在行...上声明的loadModue。
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.... settings.php定义HOST,APP_ROOT等n DB数据。
I tried using 我尝试使用
if(!function_exists(loadModule)){
function loadModule(){.....}
}
but this is giving class Sql not found
fatal error ... basically not loading the classes. 但这给了
class Sql not found
致命错误...基本上是不加载类。
I tried changing the name of the function to loadModule_new
but that is giving same error of cannot redeclare
. 我试图将函数的名称更改为
loadModule_new
但这给出了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. 我在StackOverflow上发现的所有情况从一开始都不起作用,但是只有在包含在子目录中的情况下才出现此问题。
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. 每当您需要将一些代码引入作用域时,都不要转储
include('whatever.php')
。 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. 失败的话,使用
include_once
而不是include
应该可以帮助您解决这些问题。
function_exists
expects quote marks around the parameter: function_exists
期望在参数周围加上引号:
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'
我认为主要问题是您要包含的其他文件可能包含
'../includes/init.php'
Use include_once ('../includes/init.php');
使用
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
另外,在该应用的所有代码库中搜索
function loadModule
( function loadModule
,以可变数量的空格),以确保在其他地方都没有关于loadModule
的定义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.