需要访问数据库的类函数

时间:2020-12-13 19:27:18

I don't know what to search for regarding my question, so hopefully someone can help me.

我不知道如何搜索我的问题,所以希望有人可以帮助我。

What's the most efficient and shortest method of setting up a database connection class and several other classes with functions that need access to database without repeating myself?

什么是设置数据库连接类和其他几个具有需要访问数据库而不重复自己的函数的类的最有效和最短的方法?

Would there be a way to declare a variable such as $pdo that can be access from any classes?

是否有办法声明一个可以从任何类访问的变量,如$ pdo?

Or, if someone can tell me the right terminology to serach for, that would be be helpful.

或者,如果有人能告诉我正确的术语,那将会有所帮助。

Thank you.

For example:

class connection {
    PDO data connection goes here
}

class A {
    function foo() {
        //This function needs access to database
        $pdo->query($query);
    }
}

class B {
    function bar() {
        //Also needs access to database
        $pdo->query($query)
    }
}

2 个解决方案

#1


As I understand you could create a class to connect to database

据我所知,你可以创建一个连接数据库的类

class dbConnect {
//Connection goes here
}

then create a base model class extending this connection class also you could do so in the constructor function so as soon as the class is instantiated connection is set

然后创建一个扩展此连接类的基础模型类,也可以在构造函数中执行此操作,以便在实例化类时实现连接设置

class BaseModel extends dbConnect {
}

then autoload both classes every time your app runs using php's autoload ref:

然后每次使用php的autoload ref运行应用程序时自动加载两个类:

php.net/manual/en/language.oop5.autoload.php

and then extend other classes as per need

然后根据需要扩展其他类

class xyz extends BaseModel { }

类xyz扩展BaseModel {}

#2


I would create database class and extend it to other classes:

我将创建数据库类并将其扩展到其他类:

class connection {
    PDO data connection goes here
}

and class A like this,

和A类这样,

class A extends connection{
    function foo() {
        //This function needs access to database
        $pdo->query($query);
    }
}

Now class A can access class connection methods and attributes.

现在,A类可以访问类连接方法和属性。

#1


As I understand you could create a class to connect to database

据我所知,你可以创建一个连接数据库的类

class dbConnect {
//Connection goes here
}

then create a base model class extending this connection class also you could do so in the constructor function so as soon as the class is instantiated connection is set

然后创建一个扩展此连接类的基础模型类,也可以在构造函数中执行此操作,以便在实例化类时实现连接设置

class BaseModel extends dbConnect {
}

then autoload both classes every time your app runs using php's autoload ref:

然后每次使用php的autoload ref运行应用程序时自动加载两个类:

php.net/manual/en/language.oop5.autoload.php

and then extend other classes as per need

然后根据需要扩展其他类

class xyz extends BaseModel { }

类xyz扩展BaseModel {}

#2


I would create database class and extend it to other classes:

我将创建数据库类并将其扩展到其他类:

class connection {
    PDO data connection goes here
}

and class A like this,

和A类这样,

class A extends connection{
    function foo() {
        //This function needs access to database
        $pdo->query($query);
    }
}

Now class A can access class connection methods and attributes.

现在,A类可以访问类连接方法和属性。