PHP OOP 数据库设计

标签 php mysql oop mysqli

好的,这只是一个简短的问题,我可能会对此有所松懈,但我只是在寻找一些指导,因为我完全是自学成才。我做了很多阅读并尝试做很多构建——我会说我正在进入一个很好的 php、mysql 和一般网络知识的中间阶段——绝不是高级或过度自信——仍在学习。

我真的很想解决 PHP 中的 OOP,所以我想为 MySQL 创建一个很好的精简数据库包装器,只是 MySQL,我对 MySQL 最满意,我看不出有任何理由使用任何其他数据库。我不想在设计中创建任何类型的可移植性——我希望它特定于我的数据库;所以我不想使用 PDO。

所以我现在的问题是,我是否应该创建一个扩展 mysqli 的类,然后为扩展该数据库基类的数据库表创建模型类?所以 class->child = mysqli->DbBase->UsersModel ?这将需要在类中使用很多 $this 语句,不是吗?

或者我应该实例化一个 mysqli 类并将它传递给 DbBase 吗?

最佳答案

类代表了现实世界中的事物(甚至是想象中的“事物”),对吧? DB 的实例表示与该数据库的连接。模型是否与数据库连接有共同之处?并不真地。我建议在您要编写的模型类中包含数据库类的实例,因为模型使用数据库连接来访问它的数据,但不是一种数据库连接。

关于 Mysqli <-> DBClass:这实际上取决于您尝试使用该 DBClass 实现的目标 - 它是否通过一些额外的功能或其他功能扩展了 Mysqli?如果没有,请不要在那里使用继承,否则您可以使用它。


一个非常基本的例子,只是为了给你一个想法:(它实际上是一个简化但绝对不是 ActiveRecord 模式的完整版本)

abstract class DbTable {
    /* An instance of your DBClass (=Database Connection), to be used if no
     * other connection is specified. */
    protected static $_defaultDbAdapter = null;

    /* The db connection to be used by this instance. */
    protected $_dbAdapter = null;

    /* The name of the table in the database. */
    protected $_tableName = '';

    public static function setDefaultDbAdapter(DbClass $db) {
        self::$_defaultDbAdapter = $db;
    }

    public function setDbAdapter(DbClass $db) {
        $this->_dbAdapter = $db;
    }

    public function getDbAdapter() {
        if (null === $this->_dbAdapter) {
            $this->setDbAdapter(self::$_defaultDbAdapter);
        }
        return $this->_dbAdapter;
    }

    public function insert(array $data) { /*...*/ }
    public function update(array $data, $where) { /*...*/ }
    public function delete($where) { /*...*/ }
    public function select($where) { /* may e.g. return an array of DbTableRow childclass instances */ } 

    // ...
}

class Users extend DbTable {
    protected $_tableName = 'my_users_table';
}

abstract class DbTableRow {
    /* The row itself (may be not yet saved to the db!) */
    protected $_data = array();

    /* The row as it is in the database (to find differences, when calling save()). */
    protected $_cleanData = array();

    /* An instance of the table that this row belongs to. */
    protected $_table = null;

    public function __construct(DbTable $table, array $data = array()) { /*...*/ }
    public function save() { /* uses $this->_table->insert()/update() */ }
    public function __get($key) { /*...*/ }
    public function __set($key, $value) { /*...*/ }
    // ...
}

class User extends DbTableRow { }

用法:

// Make a new connection to the database
$db = new DbClass('...'); // or whatever you name that class...

// Set this connection to be the default connection
DbTable::setDefaultDbAdapter($db);

// Create a new user
$users = new Users();
$user = new User($users);
$user->email = 'test@example.com';
$user->save();

关于PHP OOP 数据库设计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9642262/

相关文章:

mysql - SQL使用同一查询从同一组数据中获取多个结果计数

php - 这个巨大的正则表达式是如何工作的?

javascript - 如果脚本已更新,如何刷新页面的缓存?

php - HTML:单引号在本网站上显示为空格,但仅在某些浏览器中显示(即在桌面上的 Chrome 中,但在 iOS 上的 Safari 中不显示)

javascript - 如何将候选人 ID 数组发送到候选人表的不同行?

oop - SWI-Prolog 中的面向对象编程

PHP PDO MySQL 插入错误,但可以在 MySQL 上直接查询

Slick 的 Mysql 版本

java - 不同的子类值能否体现在继承的方法中?

oop - 重载的 horzcat() 如何工作?