trigger_error() line number

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

trigger_error() line number

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

may want to look into creating an error handler.. or your own version of an error handling system..
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

I vote pedant...;) Create an error handler - its simple enough to add your own distintive reporting preferences that way.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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?
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post 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("&nbsp;","<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... ;)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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.. :oops:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it all circulates around debug_backtrace() ;)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Aha!

Although the use of debug_backtrace() just made php.exe throw an exception on my box :oops:

EDIT: nvm - stoopid me forgot to actually handle the exception/error and not just var_dump(debug_backtrace()) in my error handler :roll:

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 :)
Post Reply