我的问题“与MySQL连接太多”,因为我为每个函数创建了一个新对象 那需要它。
此操作的最佳解决方案是什么?还有其他方法吗?
$db = connection(); // PDO
//1
function test(){
$GLOBALS['db']->prepare(..);
}
// 2
function test2(){
global $db
$db->prepare(..);
}
答案 0 :(得分:2)
我说两者都不好,将$ db对象作为参数传递给函数:
function test($db) {
$db->prepare(...);
}