In that mood today, just thought I would post a handy script that I have written and tend to use all the time. It saves a great deal of time and energy when writing large PHP programs.

Simply put, it makes use of the __autoload function so that you never again have to load class files manually (provided you keep them in a structured directory of course). It also creates custom Exception objects on the fly. I use Exceptions all over the place, and hardly ever need more functionality than the base object gives, but I do like them to have identifiable names - so creating them this way is perfect.

Other than that, it should be self explanitory. Feel free to ask though, or even better, suggest any improvements.

cheers
Nick
PHP Code:
<?php
/**
 * This Script may be used and modified free of charge by anyone, however don't
 * hold me liable for anything that might arise from its use!
 */
  
/**
 * Hide direct access to file if it resides within a directory named '_private'
 * I find this useful as it adds the ability to move the scripts outside of such
 * a directory, and have them visible, with no changes to the source.
 *
 * Alternatively, to always hide the script if directly accessed:
 *
    if( $_SERVER['SCRIPT_NAME'] == $_SERVER['PHP_SELF'] ) {
        header("HTTP/1.0 404 Not Found");
    }
 *
 */
if(preg_match('/\/_private\//',$_SERVER['PHP_SELF'] )) { 
    
header("HTTP/1.0 404 Not Found");
}

/**
 * 'Just-in-time' Class file loader
 *
 * Auto-loads a class file derived from the class name. If the class starts with 
 * 'Exception_', and no file can be found, a new Exception class is created 
 * automatically
 *
 * @param string Class name.
 */
function __autoload($classname) {
    
//path to the php class library (relative to this file)
    
$dir 'phplib/';
    
    
/* Create the first file path to attempt to load from. underscores in class
     * names signify directories.
     * e.g. If the class is 'Database_MySQL' the file would become:
     *             $dir/Database/MySQL.class.php
     */
    
$filename $dir.str_replace("_","/",$classname).'.class.php';
    
     
//Attempt to include this file
    
if(@include_once($filename) ) {
        return;
    }
    
    
/**
     *If the first file does not exist, a second file path is attempted using a
     * slightly different convention.  This is useful if, like me, you want a
     * base class to reside within a directory of the same name.
     * e.g. If the class is 'Database_MySQL' the file would become:
     *             $dir/Database/MySQL/MySQL.class.php
     */
    
$name_arr preg_split('/_/',$classname);
    
$filename2 $dir.str_replace("_","/",$classname)."/".array_pop($name_arr)
                                                                  .
".class.php";
    
    
//Attempt to include this file
    
if(@include_once($filename2)) {
        return;
    }
    
    
/**
     *Finally, if the second file doesn't come up trumps, check the start of the
     * class name for 'Exception_', if found create the class
     * automatically, extending the Exception object.
     * This is very useful if you want to throw Exceptions with 
     * descriptive names, as it eliminates the requirement for the
     * classes to be defined manually.
     */
    
if( preg_match('/^Exception_/',$classname) ){
        eval(
"class $classname extends Exception {}");
    }
}
// End __autoload function definition
?>