0

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!

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I'll read about debug_backtrace, it sounds interesting. Using logging package is less attractive because I'll implement this error handling in many sites/servers; also I'll read them too.
If you're implementing it over and over, that's an even larger reason to use third party code. They've already done all the hard work of creating it, testing it, making sure it works, documenting it, QAing it, etc.
Zend_Log is the most attractive package although the sites I'm developing contains just a few files. I'm interested on it for bigger projects. Thank you for the recommendation.
1

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__;

2 Comments

Good point. The function will be implemented in a class, I'll reconsider how to do it.
At the end (and due to this 4-file project) this is the most simple solution I can implement, by now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.