Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need to try/catch on opendir in PHP due to E_WARNING?

Tags:

php

I'm making a little file browser for homework - and I have it working as well as I need to, but I'm a little confused on handling error scenarios in PHP now that I'm trying to refactor my coding.

I'm used to C++/C# try/catch error handling, which PHP seems to have some variant of. What's confusing me is this:

resource opendir ( string $path [, resource $context ] )

Returns a directory handle resource on success, or FALSE on failure.

If path is not a valid directory or the directory can not be opened due to permission restrictions or filesystem errors, opendir() returns FALSE and generates a PHP error of level E_WARNING. You can suppress the error output of opendir() by prepending '@' to the front of the function name.

from http://php.net/manual/en/function.opendir.php.

Do I need to catch the generated PHP error of level E_WARNING mentioned there, or is it silly to? I don't understand why it would return false and throw an error - shouldn't the error throwing make it so you don't return normally anyway?

like image 886
John Humphreys Avatar asked Sep 24 '11 20:09

John Humphreys


People also ask

What is opendir in PHP?

The opendir() function opens a directory handle.

What is Opendir used for?

Description: The opendir() function is used with readdir() and closedir() to get the list of file names contained in the directory specified by dirname. You can read more than one directory at the same time using the opendir(), readdir(), rewinddir() and closedir() functions.


3 Answers

You would use catch in order to deal with a raised Exception. A PHP error is something different. PHP errors have different levels, or severity. You can modify which of these errors is outputted using error_reporting(). In order to suppress an individual PHP error on a statement, use @, like: @opendir(..)

like image 100
Rusty Fausak Avatar answered Oct 05 '22 05:10

Rusty Fausak


Errors/warnings produced in this way cannot therefore be handled using try/catch because the language infrastructure that handles them dates back to before PHP supported exceptions or try/catch.

You'll only really be able to use try/catch when you're using the more modern PHP modules, which use classes and generate exceptions instead of errors and warnings.

Most of PHP's core functionality is still based around the old error handling mechanism.

The first thing you should be doing with opendir() is checking that the directory exists and can be opened before you attempt to open it; this will prevent you needing to handle error conditions with opendir() itself.

If you are still cautious about warnings being thrown, you can suppress the warnings using the @ symbol. Be aware that this will suppress genuine errors as well.

Alternatively, if you're really bored, you could create a wrapper for the whole thing, which changes the error_reporting() level before making the call and sets it back again afterward. You could even have it throw an exception for you if necessary.

So these things can be done, but it's unlikely to be worth the hassle; you may as well just live with the fact that PHP's error handling isn't great, and has evolved over time rather than being carefully thought out in the first place as C++/C#'s was.

like image 35
Spudley Avatar answered Oct 05 '22 06:10

Spudley


Warnings should only be thrown in test/acceptation environments. You can safely check if the directory exists by strong compared it with false

if (opendir($dir) === false)
    echo "it failed!";

The waring can be caught with a custom error handler which can write it to a log file instead showing it on the screen.

like image 31
Roger Far Avatar answered Oct 05 '22 06:10

Roger Far