在PHP函数上的和错误 [英] Cheking and error on a PHP function

查看:106
本文介绍了在PHP函数上的和错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

There is a way to check with a "IF" if a function fails in php?

例如.

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

谢谢!

推荐答案

我假设您对像getimagesize这样的函数特别感兴趣,该函数不会返回任何错误代码,因此会使我们感到不便.但并非不可能.

I 'm assuming you are interested specifically in a function like getimagesize, which does not return any error codes and makes it hard on us. But not impossible.

getimagesize的文档说明:

如果无法访问文件名图像或图像无效,则getimagesize()将生成级别为E_WARNING的错误.出现读取错误时,getimagesize()将生成级别为E_NOTICE的错误.

If accessing the filename image is impossible, or if it isn't a valid picture, getimagesize() will generate an error of level E_WARNING. On read error, getimagesize() will generate an error of level E_NOTICE.

因此,您需要做两件事:

Therefore, you need to do two things:

  1. 获得有关错误的通知并采取某种措施
  2. 该错误不会显示或以其他方式影响程序的执行(毕竟,您将自己使用错误处理代码来处理所有错误)

您可以使用 set_error_handler() restore_error_handler() .您可以使用错误控制运算符 @.

因此,代码必须像这样:

So, the code must go something like this:

// 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
}

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

Of course, this is not very maintainable (you have a global variable $error_occurred there, and this is not a good practice). So for a solution which not only works but is also nicely engineered, you would package all this in a class. That class would define:

  1. 实现错误处理程序的方法(在上例中为my_error_handler).要将对象方法设置为错误处理程序而不是全局函数,您需要使用合适的第一个参数调用set_error_handler;请参见 callback文档.
  2. 一种方法,该方法使类可以设置错误处理程序,执行您选择的某些代码,保存在执行代码时发生错误"标志,并还原错误处理程序.这种方法可以例如采取您的调用代码提供的callback和参数数组,然后使用 call_user_func_array 来执行它.如果在执行过程中调用了上面#1中设置的错误处理程序,请在对象的变量中进行标记.您的方法会将call_user_func_array的返回值返回到调用代码.
  3. 调用代码可以用来访问上面#2的结果的方法或变量.
  1. A method which implements the error handler (my_error_handler in the above example). To set an object method as an error handler instead of a global function, you need to call set_error_handler with a suitable first parameter; see the documentation for callback.
  2. A method which lets the class set the error handler, execute some code of your choice, save the "error happened while executing your code" flag, and restore the error handler. This method could e.g. take a callback provided by your calling code and an array of parameters and use call_user_func_array to execute it. If, during execution, the error handler set from #1 above is called, mark this in a variable in your object. Your method would return the return value of call_user_func_array to the calling code.
  3. A method or variable which the calling code can use to access the result from #2 above.

因此,如果该类称为ErrorWatcher,则您的调用代码将类似于:

So then, if that class is called ErrorWatcher, your calling code would be something like:

$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
}

...这很好,也很整洁,不会与全局变量混淆.我希望我对此进行了充分的解释,以使您能够自己编写ErrorWatcher类. :-)

...which is nice and neat and does not mess with global variables. I hope I explained this well enough to enable you to write class ErrorWatcher yourself. :-)

这篇关于在PHP函数上的和错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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