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条)

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)