Page 1 of 1
trigger_error() line number
Posted: Thu Oct 27, 2005 3:35 am
by Jenk
Hi folks,
I'm currently writing up some classes and functions, and have been using trigger_error() to flag up inappropraire function calls, or the obvious use, errors.
I have a slight niggle with it though, and that is the error number and file name reported by the error.
For example, I have a class in a file "fooBar.php" which is included. One of the methods ('myFunc' in this example) within the class uses trigger_error() to report when the incorrect arguments are supplied, nothing fancy, just:
fooBar.php:
Code: Select all
<?php
class fooBar {
function myFunc ($var1 = false, $var2 = false)
{
if (($var === false) || ($var2 === false)) {
trigger_error('Incorrect arguments specified for method myFunc!', E_USER_NOTICE);
return false;
} else {
//do stuff...
}
}
}
?>
So when this is used, such as:
somepage.php:
Code: Select all
<?php
include_once 'fooBar.php';
$foo = new fooBar();
$foo->myFunc();
?>
It generates the error as expected, but it reports the error as:
Code: Select all
Incorrect arguments specified for method myFunc! on /path/to/fooBar.php on line: 7
What I would like, is for the error to report as (in this case):
Code: Select all
Incorrect arguments specified for method myFunc! on /path/to/somepage.php on line: 7
Is there a way to do this or should I just stop being such a pedant? :p
Posted: Thu Oct 27, 2005 10:27 am
by feyd
may want to look into creating an error handler.. or your own version of an error handling system..
Posted: Thu Oct 27, 2005 11:16 am
by Maugrim_The_Reaper
I vote pedant...

Create an error handler - its simple enough to add your own distintive reporting preferences that way.
Posted: Fri Oct 28, 2005 2:30 am
by Jenk
I did consider this, but it brought me back to the same question/problem - how can I obtain the line number and file name of the function call without manually entering them as arguments?
Posted: Fri Oct 28, 2005 2:51 am
by n00b Saibot
This is an excerpt from my library...
Code: Select all
// Define Our Own Error Handler...
function my_stylish_error_handler($nr, $text, $file, $line)
{ if (is_readable($file))
{
$source = file($file);
$source = str_replace(array(" ","<code><font color=\"#000000\">","</font>\r\n</code>"), "", highlight_string('<?'.str_replace("\r\n","",$source[$line-1]).'?>', 1));
$source = substr_replace($source, "", strpos($source, "<?"), 5);
$source = substr_replace($source, "", strrpos($source, "<?")-1, 5);
}
else
{ $source = "<i>N/A</i>"; }
catchErr("<span class=\"errEmph\">$text</span> in <span class=\"errEmph\">".str_replace('path/to/root', '', $file)."</span><br />Line $line : <code class=\"errSrc\">\r\n\r\n$source\r\n\r\n</code>"); }
set_error_handler("my_stylish_error_handler");
$nr = err_no
$text = err text
$file = file name
$line = line no.
all these are passed by PHP itself so you don't need to worry 'bout that...
PS: and yes you can use that...

Posted: Fri Oct 28, 2005 2:57 am
by Jenk
That prints the line number and file name of the script containing the trigger_error() function, not the line number and the file name of the parent function call
Thx for info though
The 'problem' is essentially with trigger_error() itself, here is what I tried (mainly just copied from the set_error_handler() examples on php.net):
Code: Select all
<?php
function myCatch ($errno, $errstr, $errfile, $errline)
{
switch ($errno) {
case E_USER_ERROR:
echo "<b>My ERROR</b> [$errno] $errstr<br />\n";
echo " Fatal error in line $errline of file $errfile";
echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n";
echo "Aborting...<br />\n";
exit(1);
break;
case E_USER_WARNING:
echo "<b>My WARNING</b> [$errno] $errstr in line $errline of file $errfile<br />\n";
break;
case E_USER_NOTICE:
echo "<b>My NOTICE</b> [$errno] $errstr in line $errline of file $errfile<br />\n";
break;
default:
echo "Unkown error type: [$errno] $errstr in line $errline of file $errfile<br />\n";
break;
}
}
set_error_handler('myCatch');
?>
when trigger_error() is called, it passes the line number and filename of the file/line that trigger_error() is on, not the file/line the parent function call is on
EDIT: Allow me to (attempt to) explain what I am hoping to achieve...
I am currently creating some classes, which of course have methods. In keeping with the whole MVC seperation ideology, I would like for this function to be able to report to the user (read: any developers) when they have a malformed function call, thus I would like the file/line pointer to be directed at their function call and not within the function itself.
Hope that adds clarity, though it probably just mixes things up more..

Posted: Fri Oct 28, 2005 4:42 am
by feyd
it all circulates around
debug_backtrace() 
Posted: Fri Oct 28, 2005 4:54 am
by Jenk
Aha!
Although the use of debug_backtrace() just made php.exe throw an exception on my box
EDIT: nvm - stoopid me forgot to actually handle the exception/error and not just var_dump(debug_backtrace()) in my error handler
Am now using:
Code: Select all
<?php
function myCatch ($errno, $errstr, $errfile, $errline)
{
$debug = debug_backtrace();
switch ($errno) {
case E_USER_ERROR:
echo "<b>My ERROR</b> [$errno] $errstr<br />\n";
echo " Fatal error in line $errline of file $errfile";
echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n";
echo "Aborting...<br />\n";
exit(1);
break;
case E_USER_WARNING:
echo "<b>My WARNING</b> [$errno] $errstr in line {$debug[2]['line']} of file {$debug[2]['file']}<br />\n";
break;
case E_USER_NOTICE:
echo "<b>My NOTICE</b> [$errno] $errstr in line {$debug[2]['line']} of file {$debug[2]['file']}<br />\n";
break;
default:
echo "Unkown error type: [$errno] $errstr in line {$debug[2]['line']} of file {$debug[2]['file']}<br />\n";
break;
}
}
set_error_handler('myCatch');
?>
Which works wonders, needs tidying though
