SwitchBox works very simply. Generally, all files that do not display output to users, but rather compute data to later be displayed and included on output pages, are kept in their own directory. For our purposes, this directory will be called ‘includes’. Now suppose we had a bunch of files in includes. It doesn’t make sense to include all these files on every page does it? What if we just needed one or two files? If we included every file all the time, we’d just be parsing a bunch of files for no reason, and thus, making the server-load skyrocket. And what if all your pages need a variable, but you have to keep it in one file that that page doesn’t really need, because redefining it in every page would be inefficient, and including that file for simply one reason would be inefficient as well. This is a bad thing, because in an ideal application there is efficiency right? So, let’s just include only the files we need on each page, and put the variables that every page needs in an included file that every page needs. We’re going to do this by creating one file. We will name this file ‘SwitchBox.php’. SwitchBox.php will take care of all of our includes for us, that’s it, just one file. This file will be included in all pages that need includes First I’ll display the code of SwitchBox.php, then I will explain how it works.
<?php
// SwitchBox.php
// Define global variables used by all pages
$url_prefix = "http://www.myserver.com/";
// Create the SwitchBox
switch ($switch) {
case "index" :
require './include/fns_database.php';
require './include/fns_index.php';
break;
case
"catalog" :
require './include/fns_database.php';
require './include/fns_catalog.php';
break;
case
"checkout" :
require './include/fns_database.php';
require './include/fns_checkout.php';
require './include/fns_email.php';
break;
// Leave default empty incase we just need global variables and no includes
default:
break;
}
?>
If you’re new to PHP or programming, that was probably very confusing to you, but it will get easier as you read on. First, we declare the global variables that every page needs, in this case, one of those variables would be $url_prefix. By doing this, every page that includes SwitchBox.php has access to those variables. Kind’ve convenient right?
Next we create the SwitchBox. The SwitchBox works around the value of one variable,
$switch
. Depending on what $switch
is defined as, the SwitchBox does something different. For instance, if $switch
was ‘index’, then the SwitchBox would include all the files required to make index.php work. And if $switch
was ‘catalog’, the SwitchBox would include all the files required to make catalog.php work. And if $switch
did not equal anything, then the SwitchBox would not do anything. We would not want our SwitchBox to do anything if we just needed the global variables, and no files. Ok so you to setup includes for a new page, all you have to do is create a new case, and include or require the files needed by that page, then break out of the switch.
Now let’s move on and find out how to use our SwitchBox.