0

The code below ensures that when a user accesses control panel, they are ran through a quick verification process to validate what their entities are. For instance, if a user is level 1 they are only given access to video feed which means nothing else is available to them.

When I look at the code though, I can see video feed being called when case 1 and 3 are called. I would possibly enjoy an alternative to make the code more efficient.

I was told a possible array could make things a little easier but then again this is faster.

    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");          
    }
3
  • This is not the case when you need to make obfuscated code to win 0.0000001sec. Each developer should write the code that is easy to read, thus - easy to optimize (but not like you did it). Commented Feb 8, 2011 at 2:27
  • 1
    I'd say this looks pretty acceptable as-is, it's certainly not inefficient for the computer. You could probably shuffle some code around, but it doesn't seem like you can fundamentally change much. Commented Feb 8, 2011 at 2:27
  • 1
    You should stop using magic numbers and define some constants instead that describe the permission levels. E.g case PERMISSION_VIDEO_FEED. Commented Feb 8, 2011 at 2:31

2 Answers 2

4

It's fine the way it is. I think you don't mean "efficiency" when you refer to the "repeated" includes. You mean you could compact your code by using the switch fall-through.

While this might make your code smaller, it has no significant impact of efficiency (the time the script takes to run) and it will actually make the code harder to read. Leave it be.

Sign up to request clarification or add additional context in comments.

8 Comments

Well efficiency would take place because the current server hosting the file(s) does not use any form of compression so less is more in my case although it all loads in 0.06 seconds.
With fall through, how would I do it without collisions? Because I know I would have to do a few switches possibly?
@tjm Certainly making the script shorter 4 lines won't make the script load measurably faster. and if when you mention compression you mention compression in the http response, that has nothing to do with this question.
well in a switch, those includes in a given case are ignored unless the case is true. And you end each one with a break. So at any point in time, php will only evaluate the cases until it finds one that is true and execute only the code for that case. So it doesn't really matter if you have the same included file even in all 4 cases, php isn't going to actually parse them all moreso than it takes to find the next case.
@tjm That sort of micro optimization will hardly bring any measurable, much less noticeable, speed improvements. Fall throughs seem to be pretty impossible, since your cases aren't supersets of each other.
|
1

Frist you may run better if you use require_once if its possible. The second point is to shorten the url it seems that its every include the same.

Maybe try to use it in a function for example:

$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);
    }
}

Wrong ? Correct me!

1 Comment

This is incredible! I forgot all about that method of fetching. I will run a few bench marks but I am favoring this a lot! It's exactly how I pictured the setup. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.