PDO try-catch 在函数中的用法
我正在考虑在我未来的所有网络应用程序中使用 PDO。 目前(使用我到目前为止所学到的知识),我在网站中处理数据库连接的是一个像这样的单例类:
class DB {
private static $instance = NULL;
private static $dsn = "mysql:host=localhost;dbname=mydatabase;";
private static $db_user = 'root';
private static $db_pass = '0O0ooIl1';
private function __construct()
{
}
private function __clone()
{
}
public static function getInstance() {
if (!self::$instance)
{
self::$instance = new PDO(self::$dsn, self::$db_user, self::$db_pass);
self::$instance-> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
return self::$instance;
}
}
和另一个文件(functions.php),其内容特定的函数看起来与这个完全一样:
function get_recent_activities ()
{
try
{
$db = DB::getInstance();
// --prepare and execute query here, fetch the result--
return $my_list_of_recent_activities;
}
catch (PDOException $e)
{
return "some fail-messages";
}
}
...
这意味着我必须在所有函数中重复 try .. catch
部分。
我的问题是:
- 我应该如何提高效率? (例如,不必在所有函数中重复
try..catch
,但仍然能够在每个函数上返回不同的“失败消息”) - 这已经是一种很好的做法吗? 我对 PDO 和 OOP 还很陌生(还有很多东西需要学习),所以(到目前为止),我真的看不到其中有任何缺点或可以改进的地方。
如果这看起来不清楚或太长,我很抱歉。 提前致谢。
I'm thinking of using PDO in all of my future webapp. Currently (using what I've learned from SO so far), what I have in my site to handle database connection is a Singleton class like this :
class DB {
private static $instance = NULL;
private static $dsn = "mysql:host=localhost;dbname=mydatabase;";
private static $db_user = 'root';
private static $db_pass = '0O0ooIl1';
private function __construct()
{
}
private function __clone()
{
}
public static function getInstance() {
if (!self::$instance)
{
self::$instance = new PDO(self::$dsn, self::$db_user, self::$db_pass);
self::$instance-> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
return self::$instance;
}
}
and another file (functions.php) with content-specific functions looking exactly like this one :
function get_recent_activities ()
{
try
{
$db = DB::getInstance();
// --prepare and execute query here, fetch the result--
return $my_list_of_recent_activities;
}
catch (PDOException $e)
{
return "some fail-messages";
}
}
...
meaning that I have to repeat the try .. catch
part in all of the functions.
My questions are :
- How should I make that more efficient ? (eg. not having to repeat
try..catch
in all functions, and yet still able to return different "fail-message" on each one) - Is this already a good practice ? I'm still new at PDO and OOP (still a lot more to learn), so (as of now), I can't really see any disadvantages or things that can be improved in there.
I'm sorry if that seems unclear or too long. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的实现很好,并且对于大多数用途来说它都可以完美工作。
没有必要将每个查询都放在 try/catch 块中,事实上在大多数情况下您实际上并不希望这样做。 这样做的原因是,如果查询生成异常,则这是致命问题(例如语法错误或数据库问题)的结果,而这些不是您执行的每个查询都应该考虑的问题。
例如:
这里的查询要么正常工作,要么根本不工作。 它根本无法工作的情况不应该在生产系统上经常出现,因此您不应该在此处检查它们。 这样做实际上会适得其反,因为您的用户会收到永远不会改变的错误,并且您不会收到提醒您该问题的异常消息。
相反,只需这样写:
一般来说,您唯一需要捕获并处理查询异常的时间是当查询失败时您想要执行其他操作时。 例如:
您需要编写一个简单的异常处理程序,用于记录或通知您生产环境中发生的数据库错误,并向用户显示友好的错误消息而不是异常跟踪。 有关以下信息,请参阅 http://www.php.net/set-exception-handler怎么做。
Your implementation is just fine, and it'll work perfectly well for most purposes.
It's not necessary to put every query inside a try/catch block, and in fact in most cases you actually don't want to. The reason for this is that if a query generates an exception, it's the result of a fatal problem like a syntax error or a database issue, and those are not issues that you should be accounting for with every query that you do.
For example:
The query here will either work properly or not work at all. The circumstances where it wouldn't work at all should not ever occur with any regularity on a production system, so they're not conditions that you should check for here. Doing so is actually counterproductive, because your users get an error that will never change, and you don't get an exception message that would alert you to the problem.
Instead, just write this:
In general, the only time that you'll want to catch and handle a query exception is when you want to do something else if the query fails. For example:
You'll want to write a simple exception handler that logs or notifies you of database errors that occur in your production environment and displays a friendly error message to your users instead of the exception trace. See http://www.php.net/set-exception-handler for information on how to do that.
这里有一些注意事项:
哦,我应该指出,这一点说明了如何包装单个函数来处理查询的所有异常处理。 我现在几乎不在其他地方编写 try catch 块,因为查询的堆栈跟踪为我提供了调试问题和修复问题所需的所有信息。
这是我当前的 PDO 包装类实现:
A couple of caveats here are:
Oh, I should point out that this point illustrates how one could wrap a single function to handle all of the exception handling for your queries. I don't write try catch blocks almost anywhere else now because the stack trace from the query gives me all of the information that I need to debug the problem and fix it.
Here is my current PDO wrapper class implementation: