exception handling - How can I reliably identify a specific error in PHP? -


because of php's unlink() not supporting exceptions natively, i'm making wrapper function it. should throw filenotfoundexception if, well, given file not deleted because doesn't exist.

for this, need determine whether error thrown unlink() caused missing file or else.

this test version custom delete function:

public function deletefile($path){     set_error_handler(function($errlevel, $errstring){         debug($errlevel);         debug($errstring);     });     unlink($path);     restore_error_handler(); } 

for $errlevel , $errstring 2 (e_warning) , unlink(/tmp/foononexisting): no such file or directory

a rather bold approach this:

if( strpos($errstring, 'no such file or directory') !== false ) {     throw new filenotfoundexception(); }; 

question 1: how can rely on error string being same across different php versions? question 2: there better way?

i simplify code:

public function deletefile($path){      if (!file_exists($path) {         throw new filenotfoundexception();     }else{         unlink($path);     }      if (file_exists($path) {         throw new filenotdeleted();     } } 

then don't have catch $errstr , complicated error catching. , work down php 4 when exceptions introduced.


Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -