How can I reliably identify a specific error in PHP?

后端 未结 4 1844
太阳男子
太阳男子 2021-01-14 19:31

Because of PHP\'s unlink() not supporting exceptions natively, I\'m making a wrapper function for it. It should throw a FileNotFoundException if, w

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-14 19:35

    I've seen php error messages change quite a bit over the years. Maybe, try to detect a change in the last error over a very granular piece of code, and then result to string parsing in a very loose manor.

    $lastErr = error_get_last();
    unlink($file);
    if ($lastErr !== error_get_last()) {
        // do something
        //maybe string parsing and/or testing with file_exists, is_writable etc...
    }
    

提交回复
热议问题