EnglishAdam
EnglishAdam

Reputation: 1390

PHP; Error reporting from inside a function

So if a function produces a warning/notice we get the LINE within the function.

I could validate before, during AND/OR after the function call but I am curious is there a way to AUTOMATICALLY be given the error producing LINE_ of the function call that called the function?

Upvotes: 0

Views: 224

Answers (2)

Rupert
Rupert

Reputation: 1639

You need to use a try catch statement like this:

try{
    //your code that errors here
}catch(Exception $e){
    echo "Line number:" . $e->getLine();
    //you could throw the exception here again
    //throw $e;
    //or create a new exception and throw that with the data you supply
    //$newException = new Exception('New message goes here', 'exception code goes here');
    //then throw it
    //throw $newException;
    exit;
}

Here is the link to the docs:

http://php.net/manual/en/exception.getline.php

You might also be interested in these methods (amongst others) that also belong to the Exception class:

  • getMessage
  • getCode
  • getFile
  • getMessage

As per your comment and creating a new exception check out the Exception class here:

http://php.net/manual/en/class.exception.php

Upvotes: 4

Kerry Kobashi
Kerry Kobashi

Reputation: 801

Use a PHP IDE with a debugger and set breakpoints, watch the call stack.

Upvotes: 0

Related Questions