Re: PEAR_Exception - some refactoring
| From: | Davey | Date: | Sat, 28 Aug 2004 19:23:51 +0000 |
| Subject: | Re: PEAR_Exception - some refactoring | ||
| References: | 1 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-33009@lists.php.net to get a copy of this message | ||
Bertrand Mansion wrote:
Hi, I have been working on PEAR_Exception and chaged a few things. You will find the new source there: <http://www.mamasam.com/pear/Exception.phps> Changes ------- - The constructor is lighter and should be faster: faster if/else, no getTrace in the constructor. - Got rid of $error_class, $error_method, $_method properties as their content is already available in method getTrace(). - method delObserver() -> removeObserver(): more comprehensive name - globals PEAR_OBSERVER_PRINT, PEAR_OBSERVER_TRIGGER, PEAR_OBSERVER_DIE become PEAR_EXCEPTION_PRINT, PEAR_EXCEPTION_TRIGGER, PEAR_EXCEPTION_DIE respectively, in order to conform to CS (there might be a PEAR_Observer class someday). - method _getCauseMessage() -> _getCauseMessage(array $causes) changed to a recursive method that fills an array of causes with arrays containing all the needed info for a informative cause message. Does not return a string anymore, just the raw data. - getErrorClass() and getErrorMethod() use the info avilable in getTrace() - And last but not least, added an html rendering as well as a more complete output for the __toString() method. The output now contains the hierarchy of all causes and a trace of where the exception was thrown. Depending on your environment (CLI or web) the output will be text or html (a table in this case). Both version contain the same info as the getTraceAsString() method, with causes messages added.I personally would prefer if for web you output <pre>$same_as_CLI</pre> rather than a table, this will help when using CGI from command line (for testing, this is what ZDE uses), so that all the HTML isn't in the way. Also means lighter output on the web :)
- Added a getTraceAsHtml() method. I'd like to commit these changes to PEAR/Exception.php if that's ok with everyone. Please let me know how you feel about it and if you have any comments on the proposed code. Example: <?php require_once 'PEAR/Exception.php'; class Test{ public function openFile($file) { if (!@fopen($file, 'r')) { throw new PEAR_Exception('Unable to open '.$file); } }} $O = new Test; try {$O->openFile('not-existing-file');} catch (PEAR_Exception $e) {echo $e;} ?> Thanks, Bertrand Mansion Mamasam