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.



-- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to