Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:2954 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 51141 invoked from network); 28 Jun 2003 17:40:03 -0000 Received: from unknown (HELO cypress.he.net) (216.218.197.2) by pb1.pair.com with SMTP; 28 Jun 2003 17:40:03 -0000 Received: from procata.com ([65.111.192.232] (may be forged)) by cypress.he.net (8.8.6p2003-03-31/8.8.2) with ESMTP id KAA26744 for ; Sat, 28 Jun 2003 10:40:00 -0700 Date: Sat, 28 Jun 2003 13:40:10 -0400 Content-Type: text/plain; charset=US-ASCII; format=flowed Mime-Version: 1.0 (Apple Message framework v552) To: internals@lists.php.net Content-Transfer-Encoding: 7bit In-Reply-To: <1056789641.11231.57.camel@hasele> Message-ID: <910BC6FE-A98F-11D7-B9F0-000502E1CD47@procata.com> X-Mailer: Apple Mail (2.552) Subject: Error handling code compatible with both PHP 4 and PHP 5 From: jeff@procata.com (Jeff Moore) References: <1056789641.11231.57.camel@hasele> I've been doing some thinking about how to write error handling code that can run in both PHP 4 and PHP 5. Under the current situation, it looks like you have to resort to the lowest common denominator of PHP 4 to make error handling code that works under both versions. Here is a proposal for changing that: 1. Change built in functions to raise exceptions. There was a thread on this. This would let you do the standard modern error handling in PHP 5: try { db_connect ( 'foo' ); } catch (exception e) { } 2. Change php 5 to convert uncaught exceptions into standard PHP 4 style errors and pass them to the current error_handler. uncaught exceptions raised by the built in functions would be converted into exactly the same parameters to the error_handler that they would have triggered in PHP 4. This maintains backward compatibility without requiring any configuration parameters. Unmodified PHP 4 code running under PHP 5 would perform exactly the same as long as no try .. catch statements were used. This PHP 4 style code would still work the same in PHP 5 even when db_connect raises an exception: set_error_handler("myErrorHandler"); db_connect('foo'); 3. Change the suppress errors operator (@) to also suppresses exceptions. This is also required for backward compatibility, so that things like this still work the same way: @db_connect('foo'); 4. Change trigger_error to raise an exception. This allows PHP 4 compatible code to communicate with PHP 5 code via exceptions. For unmodified PHP 4 code, the uncaught exception would be changed back into a standard error_handler style error. Thus, the code would run the same under PHP 4. PHP 5 code could catch the exception. A good example of this would be calling a PHP 4/PHP 5 compatible library from either a PHP 4 or a PHP 5 program. The library can simply call trigger_error. Any caller can then handle the error in the way that is appropriate: function MultiVersionCompatableLibraryFunction() { if ($Problem) trigger_error("Message"); } function PHP4Caller() { set_error_handler("myErrorHandler"); MultiVersionCompatableLibraryFunction(); } function PHP5caller() { try { MultiVersionCompatableLibraryFunction(); } catch (exception e) { } } 5. Add a call_error_handler() function This function takes an exception as a parameter and converts it to a valid call on a PHP 4 style error_handler. call_error_handler($exceptionObj, $handlerName) The second parameter would default to the current error handler. This would be used for explicitly converting between the different error models: try { ... } catch (exception e) { call_error_handler($e); } This proposal is intended to give an intermediate solution for errorhandling during the transition from PHP 4 to PHP 5. Without this, the jump from using an error_handler to using exceptions is too harsh. As soon as you start using exceptions, you have to abandon PHP 4. This won't be realistic for libraries (such as smarty) that have to support the version of PHP that people are actually using. Without the capability to mix error models, library authors will have to delay using exceptions until the majority of installations are using PHP 5. With the described changes, library authors can migrate to an transitional solution that works with both PHP 4 and PHP 5. Comments? Is this possible? Am I missing something? This proposal is a bit half-baked, but I wanted to get it out as soon as I could.