php - 由于 E_WARNING,我是否需要在 PHP 中尝试/捕获 opendir?

标签 php

我正在为家庭作业制作一个小文件浏览器 - 我已经按照我的需要让它正常工作,但是我对 PHP 中的错误场景处理有点困惑,因为我正在尝试重构我的编码。

我习惯于 C++/C# try/catch 错误处理,PHP 似乎有一些变体。令我困惑的是:

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.

来自 http://php.net/manual/en/function.opendir.php .

我是否需要捕获那里提到的 E_WARNING 级别的生成的 PHP 错误,还是这样做很愚蠢?我不明白为什么它会返回 false 并抛出错误 - 错误抛出不应该让你无论如何都不会正常返回吗?

最佳答案

您可以使用 catch 来处理引发的 Exception。 PHP 错误是不同的东西。 PHP 错误具有不同的级别或严重性。您可以使用 error_reporting() 修改输出这些错误中的哪些错误.为了抑制语句中的单个 PHP 错误,请使用 @,例如:@opendir(..)

关于php - 由于 E_WARNING,我是否需要在 PHP 中尝试/捕获 opendir?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7541621/

相关文章:

PHP OOP 接口(interface)类型继承

php - Mysql查询正常但不进入循环,PHP,MYSQL

php - 有什么方法可以阻止人们上传带有注入(inject)的 GIF 动图?

javascript - PHP 脚本 JQuery 获取错误数量的元素

php - laravel 中 https 的问题

php - 如何在php中使用base64binary

php - mysql查询速度优化

Php排行榜问题

PHP OOP 访问方法的良好实践?

php - 监控数据库中表之间的关系