class db{
public $sqlParamIndex = 0; //相当全局变量,不然在匿名函数里无法递增。
function query(){
$params = [“sb”,2];
$sql = “SELECT `value` FROM `sessions` WHERE `id` = ? AND `expire` > ?”;
$index = 0;
$that = $this;
$sqlStr = preg_replace_callback(“|(?)|”, function($match) use($that,$params){ return “‘”.addslashes($params[$that->sqlParamIndex++]).”‘”;},$sql);
echo $sqlStr;
}