简体   繁体   English

数据库类连接到我的其他类

[英]Database class to connect to my other classes

I'm following MVC OOP structure and I'm wondering how a decent database class would look like. 我正在遵循MVC OOP结构,并且想知道一个像样的数据库类是什么样子。 The purpose of the database class is to for other classes to either call its method or extend it. 数据库类的目的是让其他类调用其方法或对其进行扩展。

This is how my connection.php looks now 这就是我的connection.php现在的样子

 <?php

$db = new mysqli('localhost', 'root', '', 'audiologiska_kliniken');

if($db->connect_errno > 0){
    die('Ett fel inträffade [' . $db->connect_error . ']');
}

This a database class I started. 这是我开始的数据库类。

    <?php
include 'C:/wamp/www/mvc/include/connect.php';
class Database
{
    private $host;
    private $user;
    private $password;
    private $database;
    function __construct{
      //  if(file_exists($filename)) {include '';}
        //else throw new exception("Error!");

        $this->host=$host;
        $this->user=$user;
        $this->password=$password;
        $this->database=$database;

        $this->connect();
    }
    private function connect()
    {//Connect to the server
        if(!mysqli_connect($this->host, $this->user, $this->password, $this->database))

It doesn't look good I know, but I really don't know how to improve it further. 我知道它看起来不好,但是我真的不知道如何进一步改进它。 I would appreciate some suggestions from you guys 谢谢你们的一些建议

As Halcyon has mentioned, look into autoloading with namespaces, with composer or your own spl_autoload function. 正如Halcyon提到的那样,请研究使用命名空间,composer或您自己的spl_autoload函数进行自动加载。

You should do some investigation into other design patterns also. 您还应该对其他设计模式进行一些调查。

It's common for a singleton pattern to be used to prevent loading multiple database connections per request/response cycle. 通常使用单例模式来防止每个请求/响应周期加载多个数据库连接。

That way every time you need your database connection you could do something like 这样,每次需要数据库连接时,您都可以执行以下操作

$db = DB::getInstance();

which would return you a new instance of the connection if nothing has previously created the class, or the existing connection. 如果以前没有创建该类或现有连接,则它将为您返回该连接的新实例。

It might also be a good idea to stop the instance being cloned by people, by implementing the __cloned magic method like so: 通过实现__cloned magic方法,阻止人们克隆实例也是一个好主意:

public function __clone() {
    throw new Exception("Can't clone a singleton");
}

People argue however that singletons are bad because they introduce global state and tight coupling into an application. 人们认为单例是不好的,因为它们将全局状态和紧密耦合引入到应用程序中。 This makes them hard to test. 这使它们难以测试。 An alternative would be to look at maybe using a Service, which is something that is meant to be global within the application. 一种替代方法是查看可能使用的服务,这是在应用程序中是全局的。

http://fabien.potencier.org/article/11/what-is-dependency-injection Symfony have a good method of using services and a dependency injection container. http://fabien.potencier.org/article/11/what-is-dependency-injection Symfony具有使用服务和依赖项注入容器的良好方法。

Laravel also has a great example of a dependency injection container in the IoC Container. Laravel在IoC容器中也有一个很好的依赖注入容器示例。 It's a single class that manages class resolution. 它是管理类解析的单个类。 The framework calls its Services ServiceProviders. 该框架将其服务称为ServiceProviders。

Working like this has many strengths, such as ease of testing and loose coupling between objects in your application. 像这样工作有很多优势,例如易于测试以及应用程序中对象之间的松散耦合。

https://github.com/illuminate/database in particular Connection.php and ConnectionResolver.php are a way of implementing database connections. https://github.com/illuminate/database特别是Connection.php和ConnectionResolver.php是实现数据库连接的一种方式。 Connection.php represents the actual connection, which is created each time with a factory pattern, and the ConnectionResolver is smart enough to handle multiple connections. Connection.php代表实际的连接,该连接是每次使用工厂模式创建的,ConnectionResolver非常聪明,可以处理多个连接。 For inspiration :) 寻求灵感:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM