This class was built in 2011 or 2012, therefore it would not use in another project only serves as understanding to treat errors:
<?php
header('Content-Type: text/html; charset=iso-8859-1');
include_once ($_SERVER["DOCUMENT_ROOT"]."/Base/AutoLoad.class.php");
class MyException extends Exception{
private $AutoLoad;
private $ConstantsExceptions;
public function __construct($message, $code = 0, $line, $file){
$this->file = $file;
parent::__construct($message, $code);
}
public function CreateMessage(){
$XML = new XML("status","ISO-8859-1","1.0");
$XML->startElement("erro");
$XML->text("Codigo do Erro: ".$this->code);
$XML->endElement();
$XML->startElement("texto");
$XML->text("Mensagem do Erro: ".$this->message);
$XML->endElement();
$XML->startElement("arquivo");
$XML->text("Arquivo do Erro: ".$this->file);
$XML->endElement();
$XML->startElement("linha");
$XML->text("Linha do Erro: ".$this->line);
$XML->endElement();
$XML->endElement();
$XML->WriteXML();
}
private function ErrorType(){
$errorType = array (
E_ERROR => 'ERROR',
E_WARNING => 'WARNING',
E_PARSE => 'PARSING ERROR',
E_NOTICE => 'NOTICE',
E_CORE_ERROR => 'CORE ERROR',
E_CORE_WARNING => 'CORE WARNING',
E_COMPILE_ERROR => 'COMPILE ERROR',
E_COMPILE_WARNING => 'COMPILE WARNING',
E_USER_ERROR => 'USER ERROR',
E_USER_WARNING => 'USER WARNING',
E_USER_NOTICE => 'USER NOTICE',
E_STRICT => 'STRICT NOTICE',
E_RECOVERABLE_ERROR => 'RECOVERABLE ERROR'
);
return $errorType;
}
public function GetNomeErro(){
if(array_key_exists($this->getCode(), $this->ErrorType())){
$err = $errorType[$errno];
}else{
$err = 'CAUGHT EXCEPTION';
}
return $err;
}
public function customFunction(){
$this->WriteErrorLog();
$this->CreateMessage();
exit();
}
private function Timestamp(){
return time();
}
public function GetError(){
return $this->message;
}
private function WriteErrorLog(){
$this->ConstantsExceptions = new ConstantsExceptions();
if($this->ConstantsExceptions->GetStartLog() == true){
$log = new WriteFileExceptions($this,$this->ConstantsExcptions);
$log->ConstructLog();
}
}
}
?>
I found an example of a call using try
and catch
:
<?php
class Conexao{
static private $instance;
static public $dbi;
private function __construct(){
try{
if(!self::$dbi = mysql_connect(DB_HOSTI, DB_USERNAMEI, DB_PASSWORDI)){
throw new MyException(mysql_error(),mysql_errno(),"", end(explode("/", $_SERVER['PHP_SELF'])));
}
try{
if(!mysql_select_db(DB_DATABASEI)){
throw new MyException(mysql_error(),mysql_errno(),"",end(explode("/",$_SERVER['PHP_SELF'])));
}
}catch(MyException $e){
$e->customFunction();
}
}catch(MyException $e){
$e->customFunction();
}
}
public static function singleton(){
if(!isset(self::$instance)){
$c = __CLASS__;
self::$instance = new $c;
}
return self::$dbi;
}
}
?>
Create a Myexception class and extend the PHP Exception class within your class customize the errors : NOTICE, WARNINGS , ERROR and so on. I have a class like that here, I just don’t know where I put it. If you want I can look.
– Rafael Salomão
The following is the link to the documentation : https://secure.php.net/manual/en/language.exceptions.extending.php
– Rafael Salomão
It would be nice if you could pass me this class.
– Hugo Borges