有什么办法可以从PHP的包含文件中跳过致命错误? [英] Is there any way to skip fatal error from include file in php?

查看:198
本文介绍了有什么办法可以从PHP的包含文件中跳过致命错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在php中包含一个文件.如果该php中存在任何致命错误,那么有任何方法可以跳过该错误.

<?php
   include "somefile.php";
   echo "OK"; // Is there any way to print this OK  If there is any fatal error on somefile.php
?>

我需要包括这个somefile.php文件.它可能会返回致命错误 对于某些主机.我想为那些主机跳过此文件.

请给我建议.

解决方案

使用此方法,您可以定义自己的延续函数,以在发生致命错误时接手.这使用 register_shutdown_function() 来拦截致命错误.

用法:

function my_continuation_func($filename, $arg2) {
    // On fatal error during include, continue script execution from here.
    // When this function ends, or if another fatal error occurs,
    // the execution will stop.
}

include_try('my_continuation_func', array($filename, $arg2));
$data = include($filename);
$error = include_catch();

如果发生致命错误(如解析错误),脚本将从my_continuation_func()开始继续执行.否则,如果在解析过程中出现错误,则include_catch()返回true.

include()的任何输出(如echo 'something';)都被视为错误.除非您通过将true作为第三个参数传递给include_try()来启用输出.

此代码自动处理关闭功能中可能的工作目录更改.

您可以将其用于任何数量的包含,但无法拦截发生的第二个致命错误:执行将停止.

要包含的功能:

function include_try($cont_func, $cont_param_arr, $output = false) {
    // Setup shutdown function:
    static $run = 0;
    if($run++ === 0) register_shutdown_function('include_shutdown_handler');

    // If output is not allowed, capture it:
    if(!$output) ob_start();
    // Reset error_get_last():
    @user_error('error_get_last mark');
    // Enable shutdown handler and store parameters:
    $params = array($cont_func, $cont_param_arr, $output, getcwd())
    $GLOBALS['_include_shutdown_handler'] = $params;
}

function include_catch() {
    $error_get_last = error_get_last();
    $output = $GLOBALS['_include_shutdown_handler'][2];
    // Disable shutdown handler:
    $GLOBALS['_include_shutdown_handler'] = NULL;
    // Check unauthorized outputs or if an error occured:
    return ($output ? false : ob_get_clean() !== '')
        || $error_get_last['message'] !== 'error_get_last mark';
}

function include_shutdown_handler() {
    $func = $GLOBALS['_include_shutdown_handler'];
    if($func !== NULL) {
        // Cleanup:
        include_catch();
        // Fix potentially wrong working directory:
        chdir($func[3]);
        // Call continuation function:
        call_user_func_array($func[0], $func[1]);
    }
}

If I include a file in to php. If there is any fatal error in that php then is there any way to skip that .

<?php
   include "somefile.php";
   echo "OK"; // Is there any way to print this OK  If there is any fatal error on somefile.php
?>

I need to include this somefile.php file. It may return fatal error for some host. I want to skip this file for those host.

Please Advice me.

解决方案

With this, you can define your own continuation function that will take over in case of a fatal error. This uses register_shutdown_function() to intercept the fatal error.

Usage:

function my_continuation_func($filename, $arg2) {
    // On fatal error during include, continue script execution from here.
    // When this function ends, or if another fatal error occurs,
    // the execution will stop.
}

include_try('my_continuation_func', array($filename, $arg2));
$data = include($filename);
$error = include_catch();

If a fatal error occurs (like a parse error), script execution will continue from my_continuation_func(). Otherwise, include_catch() returns true if there was an error during parsing.

Any output (like echo 'something';) from the include() is treated as an error. Unless you enabled output by passing true as the third argument to include_try().

This code automatically takes care of possible working directory changes in the shutdown function.

You can use this for any number of includes, but the second fatal error that occurs cannot be intercepted: the execution will stop.

Functions to be included:

function include_try($cont_func, $cont_param_arr, $output = false) {
    // Setup shutdown function:
    static $run = 0;
    if($run++ === 0) register_shutdown_function('include_shutdown_handler');

    // If output is not allowed, capture it:
    if(!$output) ob_start();
    // Reset error_get_last():
    @user_error('error_get_last mark');
    // Enable shutdown handler and store parameters:
    $params = array($cont_func, $cont_param_arr, $output, getcwd())
    $GLOBALS['_include_shutdown_handler'] = $params;
}

function include_catch() {
    $error_get_last = error_get_last();
    $output = $GLOBALS['_include_shutdown_handler'][2];
    // Disable shutdown handler:
    $GLOBALS['_include_shutdown_handler'] = NULL;
    // Check unauthorized outputs or if an error occured:
    return ($output ? false : ob_get_clean() !== '')
        || $error_get_last['message'] !== 'error_get_last mark';
}

function include_shutdown_handler() {
    $func = $GLOBALS['_include_shutdown_handler'];
    if($func !== NULL) {
        // Cleanup:
        include_catch();
        // Fix potentially wrong working directory:
        chdir($func[3]);
        // Call continuation function:
        call_user_func_array($func[0], $func[1]);
    }
}

这篇关于有什么办法可以从PHP的包含文件中跳过致命错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆