October 28, 2009

A Default Config.php For All Your Projects

Wheneve rI start on a php project, I include the latest version of my config.php file from my Snippets section. This file contains my most commonly used functions and serve as the base from which I build whatever website/application I am working on. Instead of re-writing my config files differently for each application, I have a common setup that works for me. Here’s what it looks like.

  • Application Information
  • Variables
  • Functions

It’s very simple and I’ve included all the code below with numerous comments, as well as included the config.php file for download.

<?php
/*
 * @file		Config.php
 * @description	contains all essential functions and configuration options
 */
 
/*
 * Logging Directory
 */
define('LOG_DIR','logs/');
 
/*
 * Enable Logging?
 */ 
define('LOG',true);
 
/*
 * The database variables below configure the different databases. 
 * To set the database use the $use variable
 *
 */
$db['dev']['host'] = 'localhost'; 
$db['dev']['user'] = 'root'; 
$db['dev']['pass'] = 'root';
$db['dev']['name'] = 'test_db'; 
 
$use = $db['dev']; 
 
/*
 * Default class loader. To ensure single instances of each class, never initialize a class 
 * directly. Instead, use load_class(class_name) to initiate classes. 
 * 
 *  @param	string name of class to be loaded
 * 	@param	string path to class with trailing slash included
 *  @return	object reference to object
 */
function &load_class($class,$dir = './'){
	static $instance; 
 
	if(!isset($instance[$class])){
		if(file_exists($dir.$class.'.php')){
			include($dir.$class.'.php');
			$instance[$class] = new $class(); 
			return($instance[$class]);
		}
		else
			error($class.' does not exist in the Library'); 
	}
}
 
/*
 * Displays and optionally logs error messages
 * 
 * @param	string the message to be displayed
 * @param 	bool enables logging for particular message
 */
function error($message,$show = false){
 
	if(LOG){
		$file = LOG_DIR.''.date('d_m_y').'.log'; 
		$fp = fopen($file,'a'); 
		if(fwrite($fp,date('h:i:s a',time())."\t-\t".$message."\t".$_SERVER['PHP_SELF']."\r\n")){
			fclose($fp); 
		}
	}
	if($show){
		echo '<div class="error">'.$message.'</div>'; 
	}
}
?>

The only things that I feel need some clarification is the load_class() function and the error() function. The load_class() function will accept two different arguments. The first one is necessary and is the name of the class to be loaded. The second is the path to the class. It is not required if the class is in the same directory as the page being run. You would use it like this:

<?php 
$myclass = load_class('myclass','path/to/class/');
?>

The error() function also accepts two arguments. The first is the message that you want to be logged. The second is whether or not to force the system to display that particular message. Remember, you can still display a message a not log it. Logging is based on the LOG variable that is defined near the beginning of the file.

<?php
error('This error message will be logged only!'); 
error('This error message will be logged AND displayed!',true);
?>

Download The Config.php File from here!

Comments (14)

  1. November 2, 2009
    felipe1982 said...

    licence? GPLv3?

    • November 2, 2009
      xangelo said...

      No license. It’s more of a tutorial and a starting point, so I didn’t see the need to append an official license to it.

  2. November 2, 2009
    felipe1982 said...

    this looks fun! can’t wait to use!

    • November 2, 2009
      xangelo said...

      Thanks, if you have any questions about it, or suggestions to make it better, let me know.

  3. November 2, 2009
    Zan said...

    There is just a problem with $file = LOG_DIR.”.date(‘d_m_y’).’.log’;

    It must be LOGDIR not LOG_DIR, … Or if you define another way :)

    • November 2, 2009
      xangelo said...

      You’re right Zan, I’ve modified the define(‘LOGDIR’,'logs/’) line to define(‘LOG_DIR’,'logs/’)

  4. November 9, 2009
    A1Services said...

    I especially like the way you go about the error handling, good tut

    • November 9, 2009
      xangelo said...

      Thanks!

  5. November 9, 2009
    George said...

    Taking this:

    As the default value for the segund argument of error() is “true”, both will be shown on screen, unless “false” is used.

    Cheers

    • November 9, 2009
      xangelo said...

      Actually George, because of what that function does, the first block will only write the error to a log file. The second block will decide to display the error on screen.

      • November 10, 2009
        George said...

        Exactly. But both cases will display the error message on screen, since the default value for the second argument of error()’s declaration is ‘true’:

        error(‘This error message will be logged only!’, false);
        error(‘This error message will be logged AND displayed!’[,true]);

        • November 10, 2009
          xangelo said...

          Ah, I think I see the issue now. Logging the issue doesn’t display the message automatically. It just writes the error to a log file which can then be checked at a later time. The logfile gives the time and date that the error occurred as well as the error message displayed. The “show” variable is just a way for you to show the message to the user to let them know that something happened. Therefore you can have logging enabled, but pass show as false to just write the error messages to the file, or pass show as true to display the error message in addition to writing it to the file.

          • November 10, 2009
            George said...

            That’s right. I was just warning you about this piece of code:

            ___
            “The error() function also accepts two arguments. The first is the message that you want to be logged. The second is whether or not to force the system to display that particular message. Remember, you can still display a message a not log it. Logging is based on the LOG variable that is defined near the beginning of the file.

            -?php
            error(‘This error message will be logged only!’);
            error(‘This error message will be logged AND displayed!’,true);
            ?-
            ___

            In your example, both would be logged (the constant is set to ‘true’), AND both would be shown on screen, since the first error() lack the second argument (default ‘true’), and the second one is ‘true’, again! :-)

            For the first case to be logged only, you should use something like:

            error(‘This error message will be logged only!’, false);

            Cheers,
            George

          • November 10, 2009
            xangelo said...

            Ah, thanks for pointing that out George. I’ve made the appropriate change!

Leave a Reply