在 PHP 中安全地捕获“允许的内存大小已用尽"错误

Safely catch a #39;Allowed memory size exhausted#39; error in PHP(在 PHP 中安全地捕获“允许的内存大小已用尽错误)
本文介绍了在 PHP 中安全地捕获“允许的内存大小已用尽"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个网关脚本,可以将 JSON 返回给客户端.在脚本中,我使用 set_error_handler 来捕获错误并且仍然有一个格式化返回.

I have a gateway script that returns JSON back to the client. In the script I use set_error_handler to catch errors and still have a formatted return.

它受允许的内存大小耗尽"错误的影响,而不是使用诸如 ini_set('memory_limit', '19T'),我只想返回用户应该尝试其他东西,因为它使用了很多内存.

It is subject to 'Allowed memory size exhausted' errors, but rather than increase the memory limit with something like ini_set('memory_limit', '19T'), I just want to return that the user should try something else because it used to much memory.

有什么好的方法可以捕捉到致命错误?

Are there any good ways to catch fatal errors?

推荐答案

正如 this answer 所建议的,您可以使用 register_shutdown_function() 注册一个回调来检查 error_get_last().

As this answer suggests, you can use register_shutdown_function() to register a callback that'll check error_get_last().

您仍然需要管理从违规代码生成的输出,无论是通过 @ (shut up) 操作符,还是 ini_set('display_errors', false)

You'll still have to manage the output generated from the offending code, whether by the @ (shut up) operator, or ini_set('display_errors', false)

ini_set('display_errors', false);

error_reporting(-1);

set_error_handler(function($code, $string, $file, $line){
        throw new ErrorException($string, null, $code, $file, $line);
    });

register_shutdown_function(function(){
        $error = error_get_last();
        if(null !== $error)
        {
            echo 'Caught at shutdown';
        }
    });

try
{
    while(true)
    {
        $data .= str_repeat('#', PHP_INT_MAX);
    }
}
catch(Exception $exception)
{
    echo 'Caught in try/catch';
}

运行时,输出Caught at shutdown.不幸的是,ErrorException 异常对象没有被抛出,因为致命错误触发了脚本终止,随后仅在关闭函数中被捕获.

When run, this outputs Caught at shutdown. Unfortunately, the ErrorException exception object isn't thrown because the fatal error triggers script termination, subsequently caught only in the shutdown function.

您可以查看shutdown函数中的$error数组,了解具体原因,并做出相应的响应.一个建议可能是针对您的 Web 应用程序重新发出请求(在不同的地址,或者当然使用不同的参数)并返回捕获的响应.

You can check the $error array in the shutdown function for details on the cause, and respond accordingly. One suggestion could be reissuing the request back against your web application (at a different address, or with different parameters of course) and return the captured response.

我建议保持 error_reporting() 高(-1 的值),并使用(正如其他人建议的那样) 使用 set_error_handler()ErrorException 处理其他所有错误.

I recommend keeping error_reporting() high (a value of -1) though, and using (as others have suggested) error handling for everything else with set_error_handler() and ErrorException.

这篇关于在 PHP 中安全地捕获“允许的内存大小已用尽"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)