当前位置: 首页 > 图文教程 > 网络编程 > PHP > 利用PHP的OOP特性实现数据保护(3)

PHP
PHP新手总结的PHP基础知识
php实现gb2312和unicode间编码转换
用php语言实现数据库连接详细代码介绍
详细解析 PHP 向 MySQL 发送数据过程
利用PHP V5开发多任务应用程序
详细讲解PHP中缓存技术的应用
php escapeshellcmd多字节编码漏洞
《PHP设计模式介绍》导言
《PHP设计模式介绍》第一章 编程惯用法
《PHP设计模式介绍》第二章 值对象模式
《PHP设计模式介绍》第三章 工厂模式
《PHP设计模式介绍》第四章 单件模式
《PHP设计模式介绍》第五章 注册模式
《PHP设计模式介绍》第六章 伪对象模式
《PHP设计模式介绍》第七章 策略模式
《PHP设计模式介绍》第八章 迭代器模式
《PHP设计模式介绍》第九章 观测模式
《PHP设计模式介绍》第十章 规范模式
《PHP设计模式介绍》第十一章 代理模式
《PHP设计模式介绍》第十二章 装饰器模式

利用PHP的OOP特性实现数据保护(3)


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 118 ::
收藏到网摘: n/a

 函数execute

  最后,还需要构建函数execute(),函数execute()编译query并且使用DB对象执行它,而DB对象在此是用于初始化DBQuery对象的。请注意在例4中,是怎样运用函数call_user_func_array()来得到编译后的query的,而这样做的原因是,函数execute()要直到运行时,才能确定传递给它的参数数目。

  例4:execute()函数

/**
*
* 执行当前query,并把占位符替换为所提供的参数。
*
* @param mixed $queryParams,... Query parameter
* @return resource A reference to the resource representing the executed query.
*/
public function execute($queryParams = '')
{
 //例如:SELECT * FROM table WHERE name=:1S AND type=:2I AND level=:3N
 $args = func_get_args();

 if ($this->stored_procedure) {
  /* 调用函数compile以取得query */
  $query = call_user_func_array(array($this, 'compile'), $args);
 } else {
  /* 如果存储过程未被初始化,就把它作为标准query执行。*/
  $query = $queryParams;
 }

 $this->result = $this->db->query($query);

 return $this->result;
}

  全部整合起来

  为演示怎样使用query对象,下面构造了一个小例子,其将把DBQuery对象作为存储过程使用,并检查是否输入了正确的用户名与密码,请看例5:

  例5:

require 'mysql_db.php5';
require_once 'query2.php5';


$db = new MySqlDb;
$db->connect('host', 'username', 'pass');
$db->query('use content_management_system');

$query = new DBQuery($db);

$query->prepare('SELECT fname,sname FROM users WHERE username=:1S AND pword=:2S AND expire_time<:3I');

if ($result = $query->execute("visualad", "apron", time())) {
 if ($db->num_rows($result) == 1) {
  echo('凭证正确。');
 } else {
  echo('凭证不正确,会话已过期。');
 }
} else {
 echo('执行query时发生错误:' . $db->error());
}

  在本文中,你已看到了如何在声明类变量时,利用访问修饰符private、protected和public,保护数据和限制数据对象的可见性,同时,在PHP 5中,这些概念也可用于其他的数据类,保护其重要的内部数据。