How can I reliably identify a specific error in PHP?

后端 未结 4 1857
太阳男子
太阳男子 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:46

    I would simplify the code:

    public function deleteFile($path){
    
        if (!file_exists($path) {
            throw new FileNotFoundException();
        }else{
            unlink($path);
        }
    
        if (file_exists($path) {
            throw new FileNotDeleted();
        }
    }
    

    Then you don't have to catch the $errstr and do complicated error catching. And it will work down to PHP 4 when exceptions were introduced.

提交回复
热议问题