我曾经读到静态类非常困难,甚至不可能调试。这是真的吗?为什么?

发布于 2024-09-12 19:55:29 字数 1283 浏览 5 评论 0原文

我曾经读到静态类非常困难,甚至不可能调试。这是真的吗?为什么?

如果一个例子有帮助,这里是我用来访问数据库的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

菊凝晚露 2024-09-19 19:55:29

只要你的静态类有方法但没有数据,它就只是一个命名空间。那里没问题。但是,如果您有静态数据,您会遇到与全局变量相同的问题:您无法再通过查看本地信息来了解系统的行为。特别是在多线程环境中,您可能会遇到意外行为和困难的调试。

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.

下雨或天晴 2024-09-19 19:55:29

我认为您读到的内容与测试这些课程有关。

有关此问题以及更多内容的深入解释,我建议您阅读 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.

The basic issue with static methods is
they are procedural code. I have no
idea how to unit-test procedural code.
Unit-testing assumes that I can
instantiate a piece of my application
in isolation. During the instantiation
I wire the dependencies with
mocks/friendlies which replace the
real dependencies. With procedural
programing there is nothing to “wire”
since there are no objects, the code
and data are separate.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文