如何避免使用global
访问 php 函数中的变量。我知道我可以将它们存储为常量,但我的变量包含 sql 并且很多。像这样...
$selectSql = 'select name from table where name = :thisName' and year = :thisYear;
$selectSqlBind = array(":thisName" => $thisName,":thisYear" => $thisYear);
这是我目前的做法
function myFunction (){
global $selectSql;
global $selectSqlBind;
$addUser = $conn->prepare($selectSql);
$addUser->execute($selectSqlBind);
//Other Stuff goes on
}
我也可以
function myFunction ($selectSql,$selectSqlBind){
$addUser = $conn->prepare($selectSql);
$addUser->execute($selectSqlBind);
//Other Stuff goes on
}
但我该怎么办
function myFunction (){
$addUser = $conn->prepare($selectSql);
$addUser->execute($selectSqlBind);
//Other Stuff goes on
}