Home  >  Article  >  php教程  >  Utilizing PHP's OOP features to achieve data protection (3)

Utilizing PHP's OOP features to achieve data protection (3)

黄舟
黄舟Original
2016-12-21 11:00:511206browse

Function execute

Finally, you need to build the function execute(). The function execute() compiles the query and executes it using the DB object, which is used to initialize the DBQuery object. Please note in Example 4 how the function call_user_func_array() is used to obtain the compiled query. The reason for this is that the function execute() cannot determine the number of arguments passed to it until runtime.

 Example 4: execute() function

/**
*
* 执行当前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;
}

All integrated

To demonstrate how to use the query object, a small example is constructed below, which will use the DBQuery object as a stored procedure and check whether the correct user name is entered and password, please see Example 5:

Example 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(&#39;SELECT fname,sname FROM users WHERE username=:1S AND pword=:2S AND expire_time<:3I&#39;);

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

In this article, you have seen how to use the access modifiers private, protected, and public to protect data and limit the visibility of data objects when declaring class variables. ,At the same time, in PHP 5, these concepts can also be used in other data classes to protect their important internal data.

The above is the content of using PHP’s OOP features to achieve data protection (3). For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn