PHP函数的cheking和错误

Dom*_*oSL 1 php

如果函数在php中失败,有一种方法可以检查"IF"吗?

防爆.

If (getimagesize($image) returns and error)  {
echo 'Error: function dont work';
}
else
{
// do something
}
Run Code Online (Sandbox Code Playgroud)

谢谢!

Jon*_*Jon 5

我假设你特别感兴趣的是一个函数getimagesize,它不会返回任何错误代码并使我们很难.但并非不可能.

getimagesize各州的文件:

如果无法访问文件名图像,或者如果它不是有效图片,getimagesize() 则会产生级别错误E_WARNING.在读取错误时,getimagesize()将生成级别错误E_NOTICE.

因此,您需要做两件事:

  1. 收到错误通知并以某种方式对其采取行动
  2. 根本没有显示错误或以其他方式影响程序的执行(毕竟,您将自己处理错误处理代码中的任何错误)

你可以使用set_error_handler()和实现第一个restore_error_handler().您可以使用错误控制运算符 实现第二个@.

所以,代码必须是这样的:

// Set our own error handler; we will restore the default one afterwards.
// Our new error handler need only handle E_WARNING and E_NOTICE, as per
// the documentation of getimagesize().
set_error_handler("my_error_handler", E_WARNING | E_NOTICE);

// No error has occured yet; it is the responsibility of my_error_handler
// to set $error_occurred to true if an error occurs.
$error_occurred = false; 

// Call getimagesize; use operator @ to have errors not be generated
// However, your error handler WILL STILL BE CALLED, as the documentation
// for set_error_handler() states.
$size = @getimagesize(...);

// At this point, my_error_handler will have run if an error occurred, and
// $error_occurred will be true. Before doing anything with it, restore the
// previous error handler
restore_error_handler();

if($error_occurred) {
    // whatever
}
else {
    // no error; $size holds information we can use
}


function my_error_handler($errno, $errstr, $file, $line) {
    global $error_occurred;

    // If the code is written as above, then we KNOW that an error
    // here was caused by getimagesize(). We also know what error it was:
    switch($errno) {
        case E_WARNING: // Access to image impossible, or not a valid picture
        case E_NOTICE:  // Read error
    }

    // We could also check what $file is and maybe do something based on that,
    // if this error handler is used from multiple places. However, I would not
    // recommend that. If you need more functionality, just package all of this
    // into a class and use the objects of that class to store more state.

    $error_occurred = true;
    return true; // Do not let PHP's default error handler handle this after us
}
Run Code Online (Sandbox Code Playgroud)

当然,这不是很容易维护(你有一个全局变量$error_occurred,这不是一个好习惯).因此,对于不仅可以工作而且设计精良的解决方案,您可以将所有这些打包在一个类中.该课程将定义:

  1. 一种实现错误处理程序的方法(my_error_handler在上例中).要将对象方法设置为错误处理程序而不是全局函数,您需要set_error_handler使用合适的第一个参数进行调用; 请参阅文档callback.
  2. 一种方法,让类设置错误处理程序,执行您选择的一些代码,保存"执行代码时发生的错误"标志,并恢复错误处理程序.该方法可以例如callback由您的调用代码和参数数组提供,并用于call_user_func_array执行它.如果在执行期间调用上面#1中设置的错误处理程序,请在对象的变量中标记它.您的方法将返回call_user_func_array调用代码的返回值.
  3. 调用代码可用于访问上述#2结果的方法或变量.

那么,如果该类被称为ErrorWatcher,那么您的调用代码将类似于:

$watcher = new ErrorWatcher;
$size = $watcher->watch("getimagesize",
                        array( /* params for getimagesize here */ ));

// $size holds your result, if an error did not occur;
// check for errors and we 're done!

switch($watcher->report_last_error()) {
    // error handling logic here
}
Run Code Online (Sandbox Code Playgroud)

...这很好,整洁,不会弄乱全局变量.我希望我能够很好地解释这一点,让你自己编写类ErrorWatcher.:-)