// you’re reading...

PHP

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!

Discussion

14 comments for “A Default Config.php For All Your Projects”

  1. licence? GPLv3?

    Posted by felipe1982 | November 2, 2009, 10:57 am
  2. this looks fun! can’t wait to use!

    Posted by felipe1982 | November 2, 2009, 10:57 am
  3. 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 :)

    Posted by Zan | November 2, 2009, 5:23 pm
  4. I especially like the way you go about the error handling, good tut

    Posted by A1Services | November 9, 2009, 7:00 am
  5. 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

    Posted by George | November 9, 2009, 7:33 am
    • 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.

      Posted by xangelo | November 9, 2009, 9:12 am
      • 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]);

        Posted by George | November 10, 2009, 6:59 am
        • 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.

          Posted by xangelo | November 10, 2009, 9:37 am
          • 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

            Posted by George | November 10, 2009, 10:22 am
          • Ah, thanks for pointing that out George. I’ve made the appropriate change!

            Posted by xangelo | November 10, 2009, 10:31 am

Post a comment