-
慕神8447489
使用面向对象编程,把指针存储到属性中
-
摇曳的蔷薇
数据库的操作,最好放到模型里面。
-
慕森卡
global $database;function demo()use($database){}
-
白衣染霜花
class DB {
private $database;
function init() {
$this->_database = ...;
}
}
$database 現在可以在DB這個class裏任意function內使用$this->_database調用,可以賦值和取值。
-
函数式编程
在你的代码基础上最简单的改法
function get_db() {
global $database;
return $database;
}
-
慕沐林林
把 $database 改成 $GLOBALS['database'] 就可以了
$database = new \StdClass();
test();
function test()
{
var_dump($GLOBALS['database']);
}
-
POPMUISE
建议使用数据库模型解决