douchensi8625 2014-11-08 07:56
浏览 63
已采纳

通过多个类的PHP Mysqli连接

So, I'm old to PHP, but most of the time I've used procedural style. I've always tried to follow a MVC pattern, even though I was doing stuff procedurally. Also, I'm used to some frameworks like CakePHP and CodeIgniter. However, I've not yet done anything from scratch using OOP. Today I was doing so for a college work, and I got to a few questions.

Usually, in frameworks, we just do $this->database->query(), on any controller class, and it works. On procedural, the $link to the database goes global scope, so you don't have to worry. I was looking for a way to do that with every class on my code, but to no success. The best I could get was using a Singleton class to instantiate the database, however, I needed to retrieve the database link in every single class. So for instance, in class Users I'd have to get it in the constructor, and at the Main class, and in every other one.

My questions is fairly simple. How do I use the database connection link through multiple classes, without having to instantiate the db in all of them. And, if doing so is the best simpler way, is there any other way to do so than the Singleton method?

I'm using Mysqli lib, by the way.

Here's the class I've been using.

class DB extends Mysqli {
    protected static $instance;

    private function __construct() {
        global $config;

        parent::__construct($config['db']['host'], $config['db']['user'], $config['db']['password'], $config['db']['database']);

        if($this->connect_error)
            die('Connect Error ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
    }

    public static function getInstance() {
        if( !self::$instance ) {
            self::$instance = new self(); 
        }
        return self::$instance;
    }
}

Here is a class usage example.

class Main {
    protected $db;

    public function __construct() {
        $this->db = DB::getInstance(); // <-- having to do that in every class is the problem
    }

    protected function start() {
        $this->db->query(something...); // works
    }
}
  • 写回答

3条回答 默认 最新

  • duanha3539 2014-11-08 19:24
    关注

    If you create a superclass with the magic __get method you can check if the name of the property requested is db and if so then automatically get the instance from your DB class and if not error out as usual:

    <?php
    class Super
    {
        public function __get($name)
        {
               if($name=="db") {
                         return DB::getInstance();
               } else {
                         trigger_error("Undefined property", E_USER_NOTICE);
               }
        }
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退