How to recover from a fatal error “Allowed memory size exhausted”

前端 未结 7 881
梦如初夏
梦如初夏 2021-01-04 08:52

Do you know any solution to recover from the PHP fatal error : \"Allowed memory size ... exhausted\"

I have a shutdown function that is called when a fatal

7条回答
  •  一生所求
    2021-01-04 09:11

    The regular way to customize error handling is through

    set_error_handler — Sets a user-defined error handler function

    The docs for this function state (emphasis mine):

    The following error types cannot be handled with a user defined function: E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING, and most of E_STRICT raised in the file where set_error_handler() is called.

    So, it won't work regularly, but you can try

    • the approach given in the comments of PHP manual, using output buffering or
    • the approach using the register_shutdown_function

    As of PHP7, Errors and Exceptions are Throwables, so you can try/catch them:

    • http://php.net/manual/en/language.errors.php7.php

提交回复
热议问题