quantme
quantme

Reputation: 3657

Improve current PHP function to handle errors

if someone could help me to improve this function to use it with this format (from scratch, not tested):

<?php
  define("LINEA", __LINE__, true);
  function line($string)
  {
    return $string . LINEA;
  }
  echo line('Error: ');
?>

Example of current use:

<?php
function line($startText, $line, $endText = NULL)
{
  $endText = !empty($endText) ? $endText : '';
  return $startText . $line . $endText;
}
/*
...
lot of code
...
*/
echo line('Error on line: ', __LINE__) . '<br />';
/*
...
lot of code
...
*/
echo line('Alert - ', __LINE__, '!');
?>

Outputs:

Error on line: 12
Alert - 18!

Upvotes: 0

Views: 124

Answers (2)

Scott Saunders
Scott Saunders

Reputation: 30394

It doesn't look like your line() function is doing you any good any more. Why don't you just call:

echo 'Error on line: ' . __LINE__;

Upvotes: 1

Charles
Charles

Reputation: 51411

You might consider using debug_backtrace to obtain information about the function caller, including line, file, class, the current scope, and much much more. This way you don't need to pass any of the line number information into your error logging function.

Be aware that generating this information can be somewhat of a drag on performance.

You should also consider using an existing logging package, like PEAR's Log, Zend_Log, or Apache log4php.

Upvotes: 2

Related Questions