On 22/05/2020 17:08, Katie Volz wrote:
I want to start a discussion on an RFC to add a declare() statement to
convert all errors triggered within a file to exceptions.


Hi Katie,


Personally, I'm not a fan of promoting messages to exceptions in this way, because APIs designed to throw exceptions generally look rather different from ones designed to warn and continue, so blindly converting seems like putting a square peg in a round hole. However, I know people do it already, so will give my thoughts on the principle.


My main concern is that it needs a tighter definition of "error", and possibly a configurable one.

The snippet from the manual which you include in your draft RFC references the current global error_reporting setting. This makes sense with a global error handler, but less so for a per-file declare - it means the caller can choose whether exceptions are thrown, somewhat defeating the point. Perhaps the declare directive could take an error level as its argument, e.g. declare(error_exceptions=E_ALL-E_NOTICE)?

Relatedly, you would need to define how this interacts with the @ (error suppression or "shut up") operator - would that force the code to run past a point that would otherwise throw an exception? Again, I think that choice should be taken away from the caller, otherwise the author needs to account for both modes, e.g.

declare(error_exceptions=E_WARNING);
try {
    $fh = fopen($blah, $mode);
    // if caller can suppress ErrorExceptions, we still need to check if $fh is false
    // ...
} catch ( ErrorException $e ) {
    // the caller shouldn't actually care what happens, because we can catch it here
}


Other than that, there's the usual awkwardness of declare - it has to be set in each file, and new directives are errors in old versions of PHP. That doesn't necessarily mean we shouldn't use it for now, though.


Regards,

--
Rowan Tommins (né Collins)
[IMSoP]

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

Reply via email to