How to access an object from another class?

2020-02-11 05:54发布

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

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?

标签: php class
7条回答
等我变得足够好
2楼-- · 2020-02-11 06:27

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

查看更多
登录 后发表回答