아래의 코드는 사용자가 제어판에 액세스 할 때 빠른 확인 프로세스를 통해 해당 항목이 유효한지 확인합니다. 예를 들어, 사용자가 레벨 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.0000001 초에 이룰 필요가있는 경우에는 그렇지 않습니다. 각 개발자는 읽기 쉽고 최적화하기 쉬운 코드를 작성해야합니다. – zerkms
나는 컴퓨터가 꽤 비효율적이지는 않다. 아마 주위에 몇 가지 코드를 뒤섞 일 수 있지만, 근본적으로 많이 바뀔 수있는 것처럼 보이지는 않습니다. – deceze
당신은 마법의 숫자 사용을 멈추고 권한 수준을 설명하는 대신에 상수를 정의해야합니다. 예 : 'PERMISSION_VIDEO_FEED'사례. – deceze