<bdo id='furKo'></bdo><ul id='furKo'></ul>

<i id='furKo'><tr id='furKo'><dt id='furKo'><q id='furKo'><span id='furKo'><b id='furKo'><form id='furKo'><ins id='furKo'></ins><ul id='furKo'></ul><sub id='furKo'></sub></form><legend id='furKo'></legend><bdo id='furKo'><pre id='furKo'><center id='furKo'></center></pre></bdo></b><th id='furKo'></th></span></q></dt></tr></i><div id='furKo'><tfoot id='furKo'></tfoot><dl id='furKo'><fieldset id='furKo'></fieldset></dl></div>

      1. <legend id='furKo'><style id='furKo'><dir id='furKo'><q id='furKo'></q></dir></style></legend>

        <tfoot id='furKo'></tfoot>
      2. <small id='furKo'></small><noframes id='furKo'>

        创建一个全局可访问的 MySQLi 对象

        时间:2023-09-24

          <bdo id='sBk60'></bdo><ul id='sBk60'></ul>
          <i id='sBk60'><tr id='sBk60'><dt id='sBk60'><q id='sBk60'><span id='sBk60'><b id='sBk60'><form id='sBk60'><ins id='sBk60'></ins><ul id='sBk60'></ul><sub id='sBk60'></sub></form><legend id='sBk60'></legend><bdo id='sBk60'><pre id='sBk60'><center id='sBk60'></center></pre></bdo></b><th id='sBk60'></th></span></q></dt></tr></i><div id='sBk60'><tfoot id='sBk60'></tfoot><dl id='sBk60'><fieldset id='sBk60'></fieldset></dl></div>
              <tbody id='sBk60'></tbody>
            <legend id='sBk60'><style id='sBk60'><dir id='sBk60'><q id='sBk60'></q></dir></style></legend>
          1. <small id='sBk60'></small><noframes id='sBk60'>

              • <tfoot id='sBk60'></tfoot>

                  本文介绍了创建一个全局可访问的 MySQLi 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有多个使用静态方法的类.这些函数使用

                  I have multiple classes that use static methods. These functions connect to the database using

                  $mysqli = new mysqli(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
                  

                  其中常量 DB_SERVER、DB_USER、DB_PASS、DB_NAME 是在全局可访问文件中定义的数据库变量.最近,我的网站开始变慢,在分析脚本后,我意识到创建对象 ($mysqli) 的调用导致了这个问题.

                  where the constants DB_SERVER, DB_USER, DB_PASS, DB_NAME are database variables defined in a globally accessible file. Recently, my site started becoming slow and after profiling the script I realized that the call to create the object($mysqli) was causing this problem.

                  我的大部分类都从 mysqli 扩展,使得

                  Most of my classes extend from mysqli such that

                  public function __construct($user_id) {
                      parent::__construct(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
                      $this->retrieve_user_details($user_id);
                      $this->check_user_account_type();
                  
                  }
                  

                  据我所知,静态方法不要使用 __construct 方法.

                  It is to my understanding that static methods DO NOT use the __construct method.

                  有人可以指导我如何创建 $mysqli 对象,以便所有需要它的静态方法都可以访问它.

                  Could someone guide me on how I can create the $mysqli object once such that it can be accessed by all static methods that require it.

                  推荐答案

                  这是一种方法:

                  创建一个可以从任何地方静态访问的单例类.

                  Create a singleton class, that can be accessed statically from anywhere.

                  class DBConnector {
                      private static $instance ;
                      public function __construct($host, $user, $password, $db){
                        if (self::$instance){
                          exit("Instance on DBConnection already exists.") ;
                        }
                      }
                  
                      public static function getInstance(){
                        if (!self::$instance){
                          self::$instance = new DBConnector(a,b,c,d) ;
                        }
                        return $instance ;
                      }
                  }
                  

                  一个例子是:

                  $mysqli = DBConnector::getInstance() ;
                  

                  但是我建议也使用另一种解决方案:

                  $mysqli = new MySQLi(a,b,c,d) ;
                  

                  然后你可以将该对象传递给其他类(构造函数)

                  Then you could pass that object to other classes (constructor)

                  class Shop {
                    private $mysqli ;
                    public function __construct(MySQLi $mysqli){
                      $this->mysqli = $mysqli ;
                    }
                  }
                  
                  $show = new Shop($mysqli) ;
                  

                  这篇关于创建一个全局可访问的 MySQLi 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:无法在 MySQLi 中通过引用传递参数 下一篇:检查 mysqli_query 是否返回任何值?

                  相关文章

                  最新文章

                  <legend id='5xeq6'><style id='5xeq6'><dir id='5xeq6'><q id='5xeq6'></q></dir></style></legend>
                1. <tfoot id='5xeq6'></tfoot>
                    <bdo id='5xeq6'></bdo><ul id='5xeq6'></ul>

                    1. <small id='5xeq6'></small><noframes id='5xeq6'>

                      <i id='5xeq6'><tr id='5xeq6'><dt id='5xeq6'><q id='5xeq6'><span id='5xeq6'><b id='5xeq6'><form id='5xeq6'><ins id='5xeq6'></ins><ul id='5xeq6'></ul><sub id='5xeq6'></sub></form><legend id='5xeq6'></legend><bdo id='5xeq6'><pre id='5xeq6'><center id='5xeq6'></center></pre></bdo></b><th id='5xeq6'></th></span></q></dt></tr></i><div id='5xeq6'><tfoot id='5xeq6'></tfoot><dl id='5xeq6'><fieldset id='5xeq6'></fieldset></dl></div>