drewbee Posted August 24, 2007 Share Posted August 24, 2007 I have a class Page which handles any given pages needed resources and output/display. IE, it does the database connections, session handling, and general html output. I want to use override the default error handler for PHP using set_error_handler() inside of this Page class, however I am having problems accessing the method errorHandler. <? function errorHandler($errno, $errstr, $errfile, $errline) { switch ($errno) { case E_USER_ERROR: echo "<b>My ERROR</b> [$errno] $errstr<br />\n"; echo " Fatal error on line $errline in file $errfile"; echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n"; echo "Aborting...<br />\n"; exit(1); break; case E_USER_WARNING: echo "<b>My WARNING</b> [$errno] $errstr<br />\n"; break; case E_USER_NOTICE: echo "<b>My NOTICE</b> [$errno] $errstr<br />\n"; break; default: echo "Unknown error type: [$errno] $errstr<br />\n"; break; } return true; } ?> However, I am having a very hard time accessing this method through set_error_handler, from within the class. I have tried: <? set_error_handler('Page::errorHandler'); set_error_handler(Page::errorHandler); set_error_handler('Page::errorHandler()'); set_error_handler(Page::errorHandler($errno, $errstr, $errfile, $errline)); set_error_handler('Page::errorHandler($errno, $errstr, $errfile, $errline)'); set_error_handler(Page::errorHandler()); set_error_handler('$this->errorHandler'); set_error_handler('Page::errorHandler()'); set_error_handler('$this->errorHandler'); set_error_handler('$this->errorHandler()'); set_error_handler('$this->errorHandler($errno, $errstr, $errfile, $errline)'); set_error_handler($this->errorHandler()); set_error_handler($this->errorHandler($errno, $errstr, $errfile, $errline)); ?> All with relatively no success. If this catches certain errors, i want to throw my custom throwError or throwWarning functions, however, the first step is finding out how to properly access my callback function through set_error_handler(); If you have any ideas, insight etc that may be useful, please let me know. The straight down and dirty question is: How do I access a callback method belonging to a class, from that class's constructor. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/66504-error-handling-in-object/ Share on other sites More sharing options...
keeB Posted August 24, 2007 Share Posted August 24, 2007 I am unsure why you would go this route instead of using Exceptions. As to your question, I have no idea, unfortunately. Never used set_error_handler before. Quote Link to comment https://forums.phpfreaks.com/topic/66504-error-handling-in-object/#findComment-333057 Share on other sites More sharing options...
drewbee Posted August 24, 2007 Author Share Posted August 24, 2007 I do use exceptions. Sort of. Probobly should use them more though, eh? I wanted to go this route as a sort-of global catch all and anything thats missed within the classes. Quote Link to comment https://forums.phpfreaks.com/topic/66504-error-handling-in-object/#findComment-333072 Share on other sites More sharing options...
448191 Posted August 26, 2007 Share Posted August 26, 2007 As with callbacks, you have to use an array: set_error_handler(array($this, 'throwError')); Quote Link to comment https://forums.phpfreaks.com/topic/66504-error-handling-in-object/#findComment-334413 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.