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.
- require 'mysql_db.php';
- require_once 'query.php';
- $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');
- try {
- if($query->execute("visualad", "apron", time()))->num_rows() == 1) {
- echo('Correct Credentials');
- } else {
- echo('Incorrect Credentials / Session Expired');
- }
- } catch (QueryException $e) {
- echo('Error executing query: ' . $e);
- }
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.
- class DBQuery
- {
- .....
-
- public function fetch_array()
- {
- if (! is_resource($this->result)) {
- throw new Exception('Query not executed.');
- }
-
- return $this->db->fetch_array($this->result);
- }
-
- public function fetch_row()
- {
- if (! is_resource($this->result)) {
- throw new Exception('Query not executed.');
- }
-
- return $this->db->fetch_row($this->result);
- }
-
- public function fetch_assoc()
- {
- if (! is_resource($this->result)) {
- throw new Exception('Query not executed.');
- }
-
- return $this->db->fetch_assoc($this->result);
- }
-
- public function fetch_object()
- {
- if (! is_resource($this->result)) {
- throw new Exception('Query not executed.');
- }
-
- return $this->db->fetch_object($this->result);
- }
-
- public function num_rows()
- {
- if (! is_resource($this->result)) {
- throw new Exception('Query not executed.');
- }
-
- return $this->db->num_rows($this->result);
- }
- }
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...