下面的代码确保当用户访问控制面板时,他们会通过快速验证过程来验证其实体是什么。例如,如果用户为1级,则只能访问视频源,这意味着他们无法使用其他任何内容。
当我查看代码时,我可以看到在调用案例1和3时调用视频源。我可能会喜欢另一种方法来提高代码效率。
有人告诉我,一个可能的阵列可以让事情变得更容易,但是再次这样会更快。
switch ($_SESSION['permission']) {
case 1: // Level 1: Video Feed
include ("include/panels/videofeed.index.php");
break;
case 2: // Level 2: Announcements / Courses / Teachers
include ("include/panels/announcements.index.php");
include ("include/panels/courses.index.php");
include ("include/panels/teachers.index.php");
break;
case 3: // Level 3: Announcements / Video Feed / Courses / Teachers / Accounts / Logs
include ("include/panels/announcements.index.php");
include ("include/panels/videofeed.index.php");
include ("include/panels/courses.index.php");
include ("include/panels/teachers.index.php");
include ("include/panels/accounts.index.php");
include ("include/panels/log.index.php");
break;
case 4: // Level 4: Teachers
include ("include/panels/teachers.index.php");
}
答案 0 :(得分:4)
它的方式很好。当你提到“重复”包括时,我认为你并不是指“效率”。您的意思是您可以使用切换功能来压缩代码。
虽然这可能会使您的代码变小,但它对效率(脚本运行的时间)没有显着影响,并且实际上会使代码更难以阅读。保持原状。
答案 1 :(得分:1)
如果你可以使用require_once
,你可以运行更好。
第二点是缩短网址似乎它的每一个都包含相同。
也许尝试在函数中使用它,例如:
$permission_id = $_SESSION['permission']
function requireModule($moduleName, $path = 'include/panels/') {
$moduleName .= '.index.php';
require_once($path . $moduleName);
}
// if you want you can add an arry for it:
$permissionModules = array(
array('videofeed'),
array('announcements', 'courses', 'teachers'),
array('announcements', 'courses', 'teachers', 'accounts', 'log', 'videofeed'),
array('teachers')
);
// now we can put it in a more effectiv way
if(array_key_exists($permission_id, $permissionModules)) {
foreach($permissionModules[$permission_id] as $includes) {
requireModule($includes);
}
}
错了吗?纠正我!