rfc:throw_error_in_extensions

PHP RFC: Throw Error in Extensions

Introduction

With PHP 7, most fatal and recoverable errors in the engine where changed to instead throw an instance of Error. However this change was only made in the engine. Extensions still use fatal and recoverable errors instead of throwing exceptions.

Proposal

This RFC proposes changing most conditions raising an E_ERROR or E_RECOVERABLE_ERROR errors to throw an instance of Error instead. Conditions due to memory errors or other unrecoverable errors will still remain fatal errors.

Below is a list of extensions and what conditions were changed to throw an instance of Error instead of a fatal or recoverable error.

  • Date:
    • Invalid serialization data for a DateTime or DatePeriod object will now throw an instance of Error from __wakeup() or __set_state() instead of resulting in a fatal error.
    • Timezone initialization failure from serialized data will now throw an instance of Error from __wakeup() or __set_state() instead of resulting in a fatal error.
  • DOM:
    • Invalid schema or RelaxNG validation contexts will throw an instance of Error instead of resulting in a fatal error.
    • Attempting to register a node class that does not extend the appropriate base class will now throw an instance of Error instead of resulting in a fatal error.
    • Attempting to read an invalid or write to a readonly property will throw an instance of Error instead of resulting in a fatal error.
  • IMAP:
    • An email address longer than 16385 bytes will throw an instance of Error instead of resulting in a fatal error.
  • Intl:
    • Failure to call the parent constructor in a class extending Collator before invoking the parent methods will throw an instance of Error instead of resulting in a recoverable fatal error.
    • Cloning a Transliterator object may will now throw an instance of Error instead of resulting in a fatal error if cloning the internal transliterator fails.
  • LDAP:
    • Providing an unknown modification type to ldap_batch_modify() will now throw an instance of Error instead of resulting in a fatal error.
  • mbstring:
    • mb_ereg() and mb_eregi() will now throw an instance of ParseError if an invalid PHP expression is provided and the 'e' option is used.
  • mcrypt:
    • mcrypt_encrypt() and mcrypt_decrypt() will throw an instance of Error instead of resulting in a fatal error if mcrypt cannot be initialized.
  • mysqli:
    • Attempting to read an invalid or write to a readonly property will throw an instance of Error instead of resulting in a fatal error.
  • Reflection:
    • Failure to retrieve a reflection object or retrieve an object property will now throw an instance of Error instead of resulting in a fatal error.
  • Session:
    • Custom session handlers that do not return strings for session IDs will now throw an instance of Error instead of resulting in a fatal error when a function is called that must generate a session ID.
    • An invalid setting for session.hash_function will throw an instance of Error instead of resulting in a fatal error when a session ID is created.
  • SimpleXML:
    • Creating an unnamed or duplicate attribute will throw an instance of Error instead of resulting in a fatal error.
  • SPL:
    • Attempting to clone an SplDirectory object will throw an instance of Error instead of resulting in a fatal error.
    • Calling ArrayIterator::append() when iterating over an object will throw an instance of Error instead of resulting in a fatal error.
  • Standard:
    • assert() will throw a ParseError when evaluating a string given as the first argument if the PHP code is invalid instead of resulting in a catchable fatal error.
    • Calling forward_static_call() outside of a class scope will now throw an instance of Error instead of resulting in a fatal error.
  • Tidy:
    • Creating a tidyNode manually will now throw an instance of Error instead of resulting in a fatal error.
  • WDDX:
    • A circular reference when serializing will now throw an instance of Error instead of resulting in a fatal error.
  • XML-RPC:
    • A circular reference when serializing will now throw an instance of Error instead of resulting in a fatal error.
  • Zip:
    • ZipArchive::addGlob() will throw an instance of Error instead of resulting in a fatal error if glob support is not available.

Backward Incompatible Changes

Generally none, though it is possible some exceptions thrown could be unintentionally caught by code written for PHP 7. However, it is rare for Error exceptions to be caught outside of cleanup or logging, so catching these exceptions is likely desirable over a fatal error.

Proposed PHP Version(s)

PHP 7.1

Voting

A majority of 50%+1 is required to approve this RFC.

Voting opened on 2016-06-27 at 4:00 pm GMT and will remain open until 2016-07-04 at 11:59 pm GMT

Merge patch for PHP 7.1
Real name Yes No
bwoebi (bwoebi)  
colinodell (colinodell)  
daverandom (daverandom)  
derick (derick)  
guilhermeblanco (guilhermeblanco)  
hywan (hywan)  
kalle (kalle)  
kguest (kguest)  
kinncj (kinncj)  
krakjoe (krakjoe)  
lcobucci (lcobucci)  
marcio (marcio)  
mariano (mariano)  
mrook (mrook)  
ocramius (ocramius)  
pauloelr (pauloelr)  
peehaa (peehaa)  
santiagolizardo (santiagolizardo)  
sebastian (sebastian)  
trowski (trowski)  
zimt (zimt)  
Final result: 21 0
This poll has been closed.

Patches and Tests

PR: #1942

References

rfc/throw_error_in_extensions.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1