您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

PHP OOP MySQL编程

PHP OOP MySQL编程

我不建议以这种方式创建数据库连接。创建一个连接,并使用该连接将其注入到对象中。您不需要为每个对象创建一个新的连接。

代码示例:

$connection = new MysqLi('localhost', 'user', 'password');

$Account = new Account($connection);

将需要更改Account为如下所示:

class Account {

    protected $connection;

    public function __construct(MysqLi $connection) {
        $this->connection = $connection;
    }

    public function register() {
        // use $this->connection for db
    }

    public function login() {
        // use $this->connection for db
    }

}

我还建议您看看有关选择MySQL APIphp.net文档。如果您真的想将OOP与PHPMysqL一起使用,则需要交换MysqLiPDO使用的API并不真正支持OOP接口。

php 2022/1/1 18:39:22 有292人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶