下面的代码确保当用户访问控制面板时,他们会通过一个快速验证过程来验证他们的实体是什么。例如,如果用户是级别1,则他们只能访问视频源,这意味着他们无法访问其他内容。
但是,当我查看代码时,我可以看到在调用case 1和case 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");
}发布于 2011-02-08 10:41:45
首先,如果可能的话,如果你使用require_once,你会运行得更好。第二点是缩短url,似乎它的每一个都包含相同的内容。
也许可以试着在函数中使用它,例如:
$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);
}
}错了吗?纠正我!
发布于 2011-02-08 10:27:57
现在的情况很好。我认为当你提到“重复的”包含时,你不是指“效率”。您的意思是您可以通过使用switch fall through来压缩您的代码。
虽然这可能会使您的代码更小,但它对效率(脚本运行所需的时间)没有显著影响,而且实际上会使代码更难阅读。别管它了。
https://stackoverflow.com/questions/4929001
复制相似问题