2011-02-08 102 views
0

下面的代码可确保当用户访问控制面板时,他们会通过快速验证过程来验证其实体是什么。例如,如果用户是1级,他们只能访问视频源,这意味着他们没有其他任何东西可用。PHP案例开关(效率)

当我查看代码时,我可以看到调用情况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

当您需要使混淆代码赢得0.0000001sec时,情况并非如此。每个开发人员都应该编写易于阅读的代码,因此 - 易于优化(但不像您所做的那样)。 – zerkms 2011-02-08 02:27:46

+1

我认为这看起来相当可接受,对于电脑来说它肯定不是*效率低下的。你也许可以随便改变一些代码,但似乎并不能从根本上改变很多。 – deceze 2011-02-08 02:27:49

+1

你应该停止使用幻数并定义一些描述权限级别的常量。例如`PERMISSION_VIDEO_FEED“。 – deceze 2011-02-08 02:31:28

回答

1

如果您使用require_once(如果可能的话),首先您可以运行更好。 第二点是缩短它的网址似乎每个都包含相同的。

也许尝试在例如一个函数使用它:

错误?纠正我!

4

这是很好的方式。当你提到“重复”包括时,我认为你不是指“效率”。你的意思是你可以通过使用切换器来压缩你的代码。

虽然这可能会使您的代码更小,但它对效率(脚本运行的时间)没有显着影响,并且实际上会使代码更难以阅读。保持它。