如何从另一个类访问对象?

时间:2021-02-23 15:59:52

I have a database class, which is used to make select, update, delete MySQL queries.

我有一个数据库类,用于进行选择,更新,删除MySQL查询。

Now, I want to create a MySQL query inside another class, but if I define $db = new DB(); in index.php, I can't use the $db var in another class. Do I have to define the variable $db over and over again, if I want to make a query? Or is there a way to make the $db var with an object global var?

现在,我想在另一个类中创建一个MySQL查询,但是如果我定义$ db = new DB();在index.php中,我不能在另一个类中使用$ db var。如果我想进行查询,是否必须一遍又一遍地定义变量$ db?或者有没有办法使用对象全局变量来生成$ db var?

7 个解决方案

#1


11  

The cleanest approach would be to aggregate the database class where needed by injecting it. All other approaches, like using the global keyword or using static methods, let alone a Singleton, is introducing tight coupling between your classes and the global scope which makes the application harder to test and maintain. Just do

最干净的方法是通过注入数据库类来聚合数据库类。所有其他方法,比如使用global关键字或使用静态方法,更不用说Singleton,正在引入类和全局范围之间的紧密耦合,这使得应用程序更难以测试和维护。做就是了

// index.php$db  = new DBClass;               // create your DB instance$foo = new SomeClassUsingDb($db); // inject to using class

and

class SomeClassUsingDb{    protected $db;    public function __construct($db)    {        $this->db = $db;    }}

Use Constructor Injection if the dependency is required to create a valid state for the instance. If the dependency is optional or needs to be interchangeable at runtime, use Setter Injection, e.g.

如果需要依赖关系来为实例创建有效状态,请使用构造函数注入。如果依赖项是可选的或者需要在运行时可以互换,请使用Setter Injection,例如,

class SomeClassUsingDb{    protected $db;    public function setDb($db)    {        $this->db = $db;    }}

#2


5  

You probably want a singleton. This gives you a way to get an instance of DB anywhere in the code. Then anywhere you want to do a query, first do $db = DB::getInstance();.

你可能想要一个单身人士。这为您提供了一种在代码中的任何位置获取DB实例的方法。然后在任何你想要查询的地方,首先要做$ db = DB :: getInstance();.

An alternative is dependency injection, which passes a DB instance to all classes which need one.

另一种方法是依赖注入,它将数据库实例传递给所有需要的实例。

#3


0  

Use the magic function __autoload().

使用魔术函数__autoload()。

#4


0  

First make your database class a singleton. And then in your new class you can do something like:

首先使您的数据库类成为单例。然后在你的新课程中你可以做一些事情:

class myNewClass{  private $_db;  public function  __construct(){     $this->_db = DB::getInstance();  }}

#5


0  

Define it on a class (separate PHP file). Then require it for every PHP file the var is needed in.

在类上定义它(单独的PHP文件)。然后需要为每个需要var的PHP文件使用它。

#6


0  

In your index.php file use

在你的index.php文件中使用

require_once('path_to_file_with_class.php');

You may also use include_once, which will give you a warning instead of an error if the 'path_to_file_with_class.php' file is not available.

您也可以使用include_once,如果'path_to_file_with_class.php'文件不可用,它将为您提供警告而不是错误。

#7


-1  

You might define it as global in your index.php file, and in the class constructor also put $this->db &= $GLOBALS['db'];

您可以在index.php文件中将其定义为全局,并且在类构造函数中也放置$ this-> db&= $ GLOBALS ['db'];

#1


11  

The cleanest approach would be to aggregate the database class where needed by injecting it. All other approaches, like using the global keyword or using static methods, let alone a Singleton, is introducing tight coupling between your classes and the global scope which makes the application harder to test and maintain. Just do

最干净的方法是通过注入数据库类来聚合数据库类。所有其他方法,比如使用global关键字或使用静态方法,更不用说Singleton,正在引入类和全局范围之间的紧密耦合,这使得应用程序更难以测试和维护。做就是了

// index.php$db  = new DBClass;               // create your DB instance$foo = new SomeClassUsingDb($db); // inject to using class

and

class SomeClassUsingDb{    protected $db;    public function __construct($db)    {        $this->db = $db;    }}

Use Constructor Injection if the dependency is required to create a valid state for the instance. If the dependency is optional or needs to be interchangeable at runtime, use Setter Injection, e.g.

如果需要依赖关系来为实例创建有效状态,请使用构造函数注入。如果依赖项是可选的或者需要在运行时可以互换,请使用Setter Injection,例如,

class SomeClassUsingDb{    protected $db;    public function setDb($db)    {        $this->db = $db;    }}

#2


5  

You probably want a singleton. This gives you a way to get an instance of DB anywhere in the code. Then anywhere you want to do a query, first do $db = DB::getInstance();.

你可能想要一个单身人士。这为您提供了一种在代码中的任何位置获取DB实例的方法。然后在任何你想要查询的地方,首先要做$ db = DB :: getInstance();.

An alternative is dependency injection, which passes a DB instance to all classes which need one.

另一种方法是依赖注入,它将数据库实例传递给所有需要的实例。

#3


0  

Use the magic function __autoload().

使用魔术函数__autoload()。

#4


0  

First make your database class a singleton. And then in your new class you can do something like:

首先使您的数据库类成为单例。然后在你的新课程中你可以做一些事情:

class myNewClass{  private $_db;  public function  __construct(){     $this->_db = DB::getInstance();  }}

#5


0  

Define it on a class (separate PHP file). Then require it for every PHP file the var is needed in.

在类上定义它(单独的PHP文件)。然后需要为每个需要var的PHP文件使用它。

#6


0  

In your index.php file use

在你的index.php文件中使用

require_once('path_to_file_with_class.php');

You may also use include_once, which will give you a warning instead of an error if the 'path_to_file_with_class.php' file is not available.

您也可以使用include_once,如果'path_to_file_with_class.php'文件不可用,它将为您提供警告而不是错误。

#7


-1  

You might define it as global in your index.php file, and in the class constructor also put $this->db &= $GLOBALS['db'];

您可以在index.php文件中将其定义为全局,并且在类构造函数中也放置$ this-> db&= $ GLOBALS ['db'];