我遇到了一些麻烦,我制作了一个动态requires函数,并在同一目录下的两个php文件中使用它,名为database_controller.php和Login.php。事实是,当我在登录文件中调用require函数时,它在测试后告诉我该文件存在,但测试后的require没有接收该文件。在我的文件database_controller上,当我调用requires函数时,它工作起来没有问题
所需的登录文件测试
require "./Utils/require.php";
if (file_exists(requires(intval($rows["role"])))) {
echo "exist";
require requires(intval($rows["role"]));
echo "loaded";
//doing something with the dynamic require
}
else {
echo requires(intval($rows["role"]));
exit;
}
当我运行登录时,我得到了错误500内部服务器错误,登录文件的响应只是"存在"
这是的内容/Utils/require.php
/**
* @function requires
* @param $userType
* @return int|string
*/
function requires($userType) {
switch($userType) {
case 0:
if (file_exists("Path to required file for user type 0")) {
return "Path to required file for user type 0";
}
else
return 0;
case 1:
if (file_exists("Path to required file for user type 1")) {
return "Path to required file for user type 1";
}
else
return 0;
case 2:
if (file_exists("Path to required file for user type 2")) {
return "Path to required file for user type 2";
}
else
return 0;
}
}
这是我在数据库控制器上做的,具有相同功能
require_once "./Utils/require.php";
require_once "./error.php";
/*
* Post request method for packers
*/
if ($UserType == 0) {
if (requires($UserType)) {
require requires($UserType);
//doing something with the dynamic required file
}
}
/*
* Post request method for supervisors
*/
elseif ($UserType == 1) {
if (requires($UserType)) {
require requires($UserType);
//doing something with the dynamic required file
}
}
/*
* Post request method for admins
*/
elseif ($UserType == 2) {
if (requires($UserType)) {
require requires($UserType);
//doing something with the dynamic required file
}
}
这里我没有500内部服务器错误,我有200 ok,我有显示的具体表格
我解决了两个同名类之间的冲突问题。在login.php上,我使用DBLogin类进行连接,在所需的文件中调用DBLogin的第二个缩进,这不是同一个类,因为两个DBLogin 之间对数据库的访问权限不同
为什么不直接使用自动加载?https://www.youtube.com/watch?v=VGSerlMoIrY
也许你只需要定义项目根的绝对路径。把它放在一个单独的文件中,并要求它在您的公共index.php 上
define('ROOT_PATH', $_SERVER['DOCUMENT_ROOT'] . '/your_project_root' );
然后在您的公共index.php中只显示
require_once'../app/the_config_file.php';
现在,当你动态地需要一些东西时,就这样做:
require_once ROOT_PATH . '/path_to_the_file.php';