}
private
function
sql_quote_replace($match)
{
$number
=
count($this->query_strings);
$this->query_strings[]
=
$match;
return
"$||$$number";
}
在此留意对静态 QUOTE_MATCH 属性 private 的使用,还有 quote_store 属性和
sql_quote_replace()函数。相比 protected,在此定义为 private 更能确保任何重载
query 类 prepare() 方 法 的 子 类 使 用 其 自 身 的 机 制 来 剔 除 引 号 。
函
数
compile
下
一
步
是
构
建
compile()
与
execute()
函
数
。
函 数
compile() 如 例
3
中 所 示 , 功 能 如 下 :
· 接 受 的 参 数 数 目 可 变 ( 即 可 变 参 数 ) , 其 将 匹 配 query 中 的 占 位 符 。
· 检 查 占 位 符 是 否 为 正 确 的 数 据 类 型 , 并 把 它 替 换 为 参 数 中 的 值 。
· 把
query
作 为 字 符 串 返 回 , 但 不 执 行 它 。
·如果 query 对象没有使用 prepare()函数初始化为一个存储过程,将抛出一个异常。
例
3
:
compile()
函
数
/**
*
返
回
编
译
的
query
,
但
并
不
执
行
它
。
*
@param
mixed
$args,...
Query
Parameters
*
@return
string
Compiled
Query
*/
public
function
compile($params)
{
if
(!
$this->stored_procedure)
{
throw new Exception(" 存 储 过 程 未 被 初 始 化 ! ");
}
/*
替
代
参
数
*/
$params = func_get_args(); //
取
得
函
数
参
数
$query
=
preg_replace("/(?query);
return $this->add_strings($query); // 把 字 符 串 放 回 query 中
}
/**
*
重
新
插
入
被
prepare()
函
数
移
除
的
字
符
串
。
*/
private
function
add_strings($string)
{
$numbers
=
array_keys($this->query_strings);
$count
=
count($numbers);
$searches
=
array();
for($x
=
0;
$x
<
$count;
$x++)
{
$searches[$x]
=
"$||${$numbers[$x]}";