The Little Guy Posted October 22, 2009 Share Posted October 22, 2009 I have a class, with a method that accepts 2 parameters. I would like to output error messages. So, lets say a new file is created, and my class is used in that file. We will call that file test.php lets say they use this method with two parameters on line 105 but... they forget to add the second parameter. I currently display this message: print("<p><b>Warning:</b> Class <b>" . __class__ . "</b> requires 2 parameters, 1 given.</p>"); Warning: Class ClassName requires 2 parameters, 1 given. I would like to change it to also include the file name and the line number, so the final result would look like this: Warning: Class ClassName requires 2 parameters, 1 given in /home/user/www/test.php on line 105. How can I get the file name and line number from within the class? Is it possible? Link to comment https://forums.phpfreaks.com/topic/178675-filename-and-line-number-custom-error/ Share on other sites More sharing options...
cags Posted October 22, 2009 Share Posted October 22, 2009 __file__ and __line__ will give you those things. Link to comment https://forums.phpfreaks.com/topic/178675-filename-and-line-number-custom-error/#findComment-942490 Share on other sites More sharing options...
The Little Guy Posted October 22, 2009 Author Share Posted October 22, 2009 My error checker is on line 25, if I place __line__ there it will give me line 25, not line 105. __file__ also does the same thing, it will be the current file, not the test.php file. Link to comment https://forums.phpfreaks.com/topic/178675-filename-and-line-number-custom-error/#findComment-942494 Share on other sites More sharing options...
Andy-H Posted October 23, 2009 Share Posted October 23, 2009 __file__ and __line__ will give you those things. They will give the filename and line number of where they are used in the class definition, I have had this problem myself and couldn't figure out a fix. Link to comment https://forums.phpfreaks.com/topic/178675-filename-and-line-number-custom-error/#findComment-942496 Share on other sites More sharing options...
cags Posted October 23, 2009 Share Posted October 23, 2009 Sorry, I thought that was what you were after. Just out of interest, it looks like your essentially emulating PHP's error messages, are you aware of the trigger_error() function? Link to comment https://forums.phpfreaks.com/topic/178675-filename-and-line-number-custom-error/#findComment-942501 Share on other sites More sharing options...
The Little Guy Posted October 23, 2009 Author Share Posted October 23, 2009 Yeah I am working with it now, but I can't get it to display the correct number and file still. Link to comment https://forums.phpfreaks.com/topic/178675-filename-and-line-number-custom-error/#findComment-942509 Share on other sites More sharing options...
jon23d Posted October 23, 2009 Share Posted October 23, 2009 I'm not 100% sure how, but I'm pretty sure you could pull it from a backtrace. Link to comment https://forums.phpfreaks.com/topic/178675-filename-and-line-number-custom-error/#findComment-942628 Share on other sites More sharing options...
Andy-H Posted October 23, 2009 Share Posted October 23, 2009 Of course Never thought of that =D debug_backtrace Link to comment https://forums.phpfreaks.com/topic/178675-filename-and-line-number-custom-error/#findComment-942893 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.