Proxy and exception customization in PHP object-oriented programming (1) _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Proxy and exception customization in PHP object-oriented programming (1 ). I. DBQuery object now, our DBQuery object simply imitates a stored procedure-once executed, it returns a result resource that must be saved; and if you want to use this 1. DBQuery object

Now, our DBQuery object simply imitates a stored procedure-once executed, it returns a result resource that must be saved; and if you want to use functions in the result set (such as num_rows () or fetch_row (), you must pass the MySqlDB object. So what if the DBQuery object is used to implement the function of MySqlDB object (which is designed to operate on a query execution result? Let's continue to use the code in the previous example. let's assume that the DBQuery object manages our result resources. The source code of the DBQuery class is shown in list 1.

List 1. use the DBQuery class.

 
 
  1. require 'mysql_db.php';
  2. require_once 'query.php';
  3. $db = new MySqlDb;
  4. $db->connect('host', 'username', 'pass');
  5. $db->query('use content_management_system');
  6. $query = new DBQuery($db);
  7. $query->prepare('SELECT fname,sname FROM users WHERE username=:1S AND pword=:2S AND expire_time<:3I');
  8. try {
  9.  if($query->execute("visualad", "apron", time()))->num_rows() == 1) {
  10. echo('Correct Credentials');
  11.  } else {
  12. echo('Incorrect Credentials / Session Expired');
  13.  }
  14. } catch (QueryException $e) {
  15.  echo('Error executing query: ' . $e);
  16. }

In the code modified above, we are most interested in catch and execute statements.

◆ The execute statement no longer returns a result resource. now it returns the DBQuery object itself.

◆ DBQuery object now implements the num_rows () function-we are familiar with it from the DB interface.

◆ If the query fails, it throws a QueryException type exception. When converted into a string, it returns the details of the error.

Therefore, you need to use the PHP proxy. In fact, you have used a proxy in our DBQuery object, but now you will use it more deeply to closely bind it to the MySqlDB object. This DBQuery object has been initialized using an object that implements the DB interface, and it already contains a member function execute-it calls the query () method of the DB object to execute this query. This DBQuery object does not actually query the database. it submits this task to the DB object. This is a proxy. it is actually a process-by sending messages to another object that implements the same or similar behavior, an object can implement a special behavior.

Therefore, you need to modify the DBQuery object to include all the functions-they operate on a result resource from the DB object. When you execute a query to call the corresponding function of the DB object and return its results, you need to use the stored results. The following functions will be added:

List 2: use a proxy to expand the DBQuery class.

 
 
  1. class DBQuery
  2. {
  3.  .....
  4.  public function fetch_array()
  5.  {  
  6. if (! is_resource($this->result)) {
  7.  throw new Exception('Query not executed.');
  8. }
  9. return $this->db->fetch_array($this->result);
  10.  }
  11.  public function fetch_row()
  12.  {
  13. if (! is_resource($this->result)) {
  14.  throw new Exception('Query not executed.');
  15. }
  16. return $this->db->fetch_row($this->result);
  17.  }
  18.  public function fetch_assoc()
  19.  {
  20. if (! is_resource($this->result)) {
  21.  throw new Exception('Query not executed.');
  22. }
  23. return $this->db->fetch_assoc($this->result);
  24.  }
  25.  public function fetch_object()
  26.  {
  27. if (! is_resource($this->result)) {
  28.  throw new Exception('Query not executed.');
  29. }
  30. return $this->db->fetch_object($this->result);
  31.  }
  32.  public function num_rows()
  33.  {
  34. if (! is_resource($this->result)) {
  35.  throw new Exception('Query not executed.');
  36. }
  37. return $this->db->num_rows($this->result);
  38.  }
  39. }

The implementation of each function is quite simple. It first checks to ensure that the query has been executed, and then proxies the task to the DB object, returning the result as if it was a query object itself (called a basic database function.1

Currently, our DBQuery object simply imitates a stored procedure-once executed, it returns a result resource that must be saved; and if you want to use this...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.