Home > Backend Development > PHP Tutorial > PHP5 OOP programming proxy and custom exception_PHP tutorial

PHP5 OOP programming proxy and custom exception_PHP tutorial

WBOY
Release: 2016-07-13 17:33:23
Original
923 people have browsed it

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 the result set function (such as num_rows() or fetch_row()), you must pass the MySQL(best combination with PHP)DB object. So, what is the effect if the DBQuery object implements the function implemented by the MySQL(the best combination with PHP)DB object (which is designed to operate on the results of a query execution)? Let's continue using the code from the previous example; and let's assume that our result resources are now managed by a DBQuery object. The source code of the DBQuery class is shown in Listing 1.

Listing 1. Using the DBQuery class.

require MySQL(the best combination with PHP)_db.php(as the current mainstream development language);
require_once query.php(As the current mainstream development language);
$db = new MySQL(The best combination with PHP)Db;
$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);
}

What we are most interested in in the modified code above are the catch statement and execute statement.

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

 · The DBQuery object now implements the num_rows() function - which we are already familiar with from the DB interface.

 · If the query execution fails, it throws an exception of type QueryException. When converted to a string, it returns the details of the error that occurred.

To do this, you need to use a proxy. In fact, you are already using proxies in our DBQuery object, but now you will use it in more depth to tightly bind it to the MySQL(best combination with PHP)DB object. The DBQuery object has been initialized with an object that implements the DB interface, and it already contains a member function execute—which calls the query() method of the DB object to execute the query. The DBQuery object itself does not actually query the database, it leaves this task to the DB object. This is a proxy, which is a process by which an object can implement a specific behavior by sending messages to another object that implements the same or similar behavior.

To do this, you need to modify the DBQuery object to include all the functions that operate on a result resource from the DB object. You need to use the stored results when executing a query to call the corresponding function of the DB object and return its results. The following functions will be added:

Listing 2: Extending the DBQuery class using proxies.

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); 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); It first checks to make sure the query has been executed, then delegates the task to the DB object, returning its results as if it were the query object itself (called a basic database function).

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/508643.htmlTechArticle1. DBQuery Object Now, our DBQuery object simply imitates a stored procedure - once executed, it returns a The resulting resource must be saved; and if you want to use that...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template