对于静态数据来说这是一个好主意吗

发布于 2024-12-21 09:21:16 字数 1164 浏览 3 评论 0原文

所以我有一个不断增长的 PHP 系统,其中有一个静态类来处理数据库连接的事情。

<?php 
    class Database {
        // ... connection upon construction and ways of escaping the data
        public function query($query) {
            // performs query and returns the data.
        }
    }

    class API { // Not actually called api, but for the purposes of this
        private static $database = false;

        public static function GetDatabase() {
            if (static::$database === false) {
                static::$database = new Database(connection information)
            }
            return static::$database;
        }
    }
?>    

我还有很多执行特定功能集的“控制器”或数据库适配器。

<?php 
    class UserDBAdapter {
        public function newUser($info) {
            // validates and builds the query statements

            API::GetDatabase()->query($query);
        }
    }
?>

所以真正的问题是我在代码中到处都需要 UserDBAdapter 。比如说在几个不同的文件中,可能在其他控制器中,我不想将它作为变量传递(当每个方法都有它时,它会变得烦人)。我也不想创建其中 2 个对象(出于速度目的)。

那么我可以做一些与 $database 对象相同的事情吗?在调用它们之前我不会初始化它们,这应该是高效的,并且无论范围如何,都不需要在整个过程中重新创建它们。至少这就是我开始这个想法的原因,但我不知道它是否是最好的想法。

谢谢

So i have an ever growing system in PHP where i have a static class that takes care of the database connection stuff.

<?php 
    class Database {
        // ... connection upon construction and ways of escaping the data
        public function query($query) {
            // performs query and returns the data.
        }
    }

    class API { // Not actually called api, but for the purposes of this
        private static $database = false;

        public static function GetDatabase() {
            if (static::$database === false) {
                static::$database = new Database(connection information)
            }
            return static::$database;
        }
    }
?>    

I also have alot of "controllers" or database adapters that perform specific sets of functionality.

<?php 
    class UserDBAdapter {
        public function newUser($info) {
            // validates and builds the query statements

            API::GetDatabase()->query($query);
        }
    }
?>

So the real question is that i need the UserDBAdapter here and there through out the code. Say in a couple different files and possibly in other controllers and i do not want to pass it in as a variable (it can get annoying when every method has it). I also do not want to create 2 of these objects (for speed purposes).

So could i do something the same as i do with $database object. I do not initialize them until they are called, which should be efficient, and they wont need to be recreated throughout an entire process, and no matter the scope. At least thats why i started this idea, but i do not know if its the best idear.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

够运 2024-12-28 09:21:16

我认为您所做的很好,您将数据库连接存储在注册表中,您可以在整个项目中轻松访问。

我想,理想情况下,您也可以在其他类中使用 setDatabase 和 getDatabase 方法覆盖它,并使用 API::getDatabase() 作为后备?

Zend Framework 有 Zend_Db_Adapter::getDefaultAdapter() ,我已经知道使用它了。或者我使用 Zend_Registry::set('dbAdapter', $dbAdapter) 将适配器分配给注册表。我知道您没有使用 Zend,但它是某人做类似事情的一个例子。

I think what you are doing is fine, you're storing the database connection in a registry you can easily access throughout your project.

I guess, ideally, you could overwrite this with setDatabase and getDatabase methods in your other classes too, with API::getDatabase() as a fallback?

Zend Framework has Zend_Db_Adapter::getDefaultAdapter() which I've been known to use.. or I assign an adapter to the registry with Zend_Registry::set('dbAdapter', $dbAdapter). I know you aren't using Zend, but its an example of someone doing something similar.

爱情眠于流年 2024-12-28 09:21:16

通常,您提到的“控制器”作为单独文件中的单独类进行管理。

有时,“单身人士”是所有应用程序中使用的一项。有时,根据其用途,有多个副本,并被称为“实体”。

您可以在这里了解更多信息:
http://en.wikipedia.org/wiki/Object-Relational_Mapping

Usually, the "controllers" you mention are managed as a separate class in a separate file.

Sometimes, are "singletons" one single item used in all application. And sometimes, have several copies, depending of it usage, and are called "entities".

You can learn more here:
http://en.wikipedia.org/wiki/Object-Relational_Mapping

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