I have 3 class name “Controller“,”Loader“,”Error” and “Ex_controller“.
“Controller.php“
class Controller
{
function __Construct()
{
$this->load = Loader::getinstant();
$this->error = $this->load->class('Error');
}
}
“Loader.php“
class Loader
{
function class($class)
{
require_once($class);
return new $class;
}
}
“Error.php“
class Error
{
function query($query)
{
$res = mysql_query($query)
if($res)
{
return $res;
}else{
die('Could not execute query: '.mysql_error().'at line '. __LINE__ .
' in file ' . __FILE__);//does it work?If it doesn't, how to
make it work?
}
}
}
“Ex_controller.php“
class Ex_controller extends Controller
{
function __Construct()
{
parent::__construct();
$result = $this->error->query('some sql query');//(*)
}
}
How can I show where the error occur in Ex_controller with (*)?
First of all, you should stop using
mysql_xxxfunctions as they’re in the process of deprecating the old API.That aside, in this case, it’s probably worthwhile to start using exceptions instead of a plain old
die().Then inside the controller:
When you catch the exception higher up and you would
print_r()it, you will see the complete stack trace with files, line numbers and everything.Plus, you get the chance to handle the error as well.
Plus plus, if you would use
PDOwith exception error handling enabled, you don’t even have to throw exceptions yourself anymore.