trigger_error - getting function call line

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
MikeG
Forum Newbie
Posts: 3
Joined: Sat Apr 28, 2007 3:44 am

trigger_error - getting function call line

Post by MikeG »

I'm using trigger_error within a class, however the line PHP outputs is the line of code trigger_error() is called at within the class. Is there a way to get it to output the line the class function was called on, as well as the file? For example:

Code: Select all

class test
{
    function myfunc()
    {
        trigger_error( 'My error message' ) // <-- I don't want this line
    }
}


test::myfunc() // <--- would it be possible to return this line number?
If I have to abandon the trigger_error function that is fine, but I'd prefer similar functionality. Thanks for the help.
Zu
Forum Commoner
Posts: 33
Joined: Wed Dec 06, 2006 4:21 am

Re: trigger_error - getting function call line

Post by Zu »

MikeG wrote:Is there a way to get it to output the line the class function was called on, as well as the file? For example:
In a way. __LINE__ and __FILE__.
MikeG
Forum Newbie
Posts: 3
Joined: Sat Apr 28, 2007 3:44 am

Post by MikeG »

__LINE__ and __FILE__ also get the line and file they're placed on. I'm looking for something automatic like the PHP functions do.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

MikeG
Forum Newbie
Posts: 3
Joined: Sat Apr 28, 2007 3:44 am

Post by MikeG »

Was hoping I didn't have to use that, but I guess that's my only other option :)

Thanks for the help
rebus
Forum Newbie
Posts: 11
Joined: Tue Nov 07, 2006 6:13 pm
Location: Croatia, Zadar

Post by rebus »

I use costum function that takes 4 arguments in order:

error level
__FILE__
__LINE__
string error message

And then call trigger_error() from that function. You dont get the exact line number but it is very close to it, and costum error message usually makes things clear.

I also like to add a configuration check for development or debug status and if it is turned on use debug_backtrace() for debugging in same function.

makes my life a lot easier and plus you can control error message formating and stuff...
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Was hoping I didn't have to use that
It's not hard

Code: Select all

$backtrace = debug_backtrace();
$called = $backtrace[$howFarBack = 1];
return $called['file']  . ' on line ' . $called['line'];
Post Reply