DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

[PHP] A Simple Error Function For Everyday Use...

05.25.2008
| 4779 views |
  • submit to reddit
        ... It uses the php function "error_log".
define("PATH_LOG","./log/");

function error($line,$method,$class,$system_error,$user_error = "",$date = "",$log = true,$show = true) {
	if (empty($date)) {
		$date = date('r');
	}
	
	if (empty($user_error)) {
		$user_error = $system_error;
	}
	
	
	$error = "$date - $method at $line - $system_error\n";
	
	if ($log == true) {
		error_log($error,3,PATH_LOG."$class.log");
	}
	
	if ($show == true) {
		echo "<div class=\"error\">$user_error</div>";
	}

	return true;
}


//Example
class Test {
	private showError true;
	
	public function __construct() {
		$test = false;
		
		if ($test === false) {
			error(__LINE__,__METHOD__,__CLASS__,"sys error blub","There are internal problems. Sorry for that.","",true,$this->showError);
		}
	}
}

$test = new Test();