我曾经读到静态类非常困难,甚至不可能调试。这是真的吗?为什么?
我曾经读到静态类非常困难,甚至不可能调试。这是真的吗?为什么?
如果一个例子有帮助,这里是我用来访问数据库的 PHP
类(不过,我不认为这是一个特定于 PHP 的问题):
<?php
class DB
{
private static $instance;
private function __construct() { }
public static function getInstance()
{
if(!self::$instance)
{
self::$instance = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME . ';', DB_USER, DB_PASS);
}
return self::$instance;
}
public static function getPreparedStatement($query)
{
$db = self::getInstance();
return $db->prepare($query);
}
public static function query($query)
{
$stmt = self::getPreparedStatement($query);
$stmt->execute();
}
public static function getResult($query)
{
$stmt = self::getPreparedStatement($query);
$stmt->execute();
return $stmt;
}
public static function getSingleRow($query)
{
$stmt = self::getPreparedStatement($query);
$stmt->execute();
return $stmt->fetch();
}
public static function getMultipleRows($query)
{
$stmt = self::getPreparedStatement($query);
$stmt->execute();
return $stmt->fetchAll();
}
}
?>
I once read that static classes are very difficult and even impossible to debug. Is this true and why?
If an example would help, here is a PHP
class I use to access a database (I don't think this is a PHP-specific question, though):
<?php
class DB
{
private static $instance;
private function __construct() { }
public static function getInstance()
{
if(!self::$instance)
{
self::$instance = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME . ';', DB_USER, DB_PASS);
}
return self::$instance;
}
public static function getPreparedStatement($query)
{
$db = self::getInstance();
return $db->prepare($query);
}
public static function query($query)
{
$stmt = self::getPreparedStatement($query);
$stmt->execute();
}
public static function getResult($query)
{
$stmt = self::getPreparedStatement($query);
$stmt->execute();
return $stmt;
}
public static function getSingleRow($query)
{
$stmt = self::getPreparedStatement($query);
$stmt->execute();
return $stmt->fetch();
}
public static function getMultipleRows($query)
{
$stmt = self::getPreparedStatement($query);
$stmt->execute();
return $stmt->fetchAll();
}
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只要你的静态类有方法但没有数据,它就只是一个命名空间。那里没问题。但是,如果您有静态数据,您会遇到与全局变量相同的问题:您无法再通过查看本地信息来了解系统的行为。特别是在多线程环境中,您可能会遇到意外行为和困难的调试。
As long as your static class has methods and no data, it's just a namespace. No problem there. But if you have static data, you run into the same problems as global variables: you can no longer understand the behavior of the system from looking at local information. Particularly in a multi-threaded environment, you could be in for unexpected behavior and difficult debugging.
我认为您读到的内容与测试这些课程有关。
有关此问题以及更多内容的深入解释,我建议您阅读 Misco 的 博客。我发现它们是宝贵的信息来源。
I would think that what you read was related to testing those classes.
For a good indepth explanation on this and much more, I'd suggest you read Misco's blog. I have found them to be a valuable source of information.