PHP5 OOP programming Agent and custom Exception _php tutorial

Source: Internet
Author: User
First, Dbquery object

Now, our Dbquery object simply mimics a stored procedure-once executed, returns a result resource that must be saved, and if you want to use a function on that result set (such as num_rows () or Fetch_row ()), you must pass MySQL ( The best combination with PHP) DB object. So what happens if the DB object (which is designed to operate on the results of a query execution) is implemented by the Dbquery object to implement MySQL (and the best combination of PHP collocation)? Let's continue using the code from the previous example, and let's assume that our result resources are now managed by the Dbquery object. The source code for the Dbquery class is shown in Listing 1.

List 1. Use 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);
}

In the modified code above, we are most interested in the catch statement and the EXECUTE statement.

· The EXECUTE statement no longer returns a result resource, and now it returns the Dbquery object itself.

· The Dbquery object now implements the Num_rows () function-we are already familiar with the DB interface.

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

To do this, you need to use a proxy. In fact, you've already used proxies in our Dbquery object, but now we'll use it more deeply to bind it tightly to MySQL (the best combination with PHP) DB objects. The Dbquery object has been initialized with an object that implements the DB interface, and it already contains a member function execute-the query () method that calls the DB object to execute it. The Dbquery object itself does not actually query the database, which is done by the DB object. This is the proxy, which is actually a process-with this process, an object can implement a particular behavior by sending the message 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-they manipulate a result resource from the DB object. When you execute a query to invoke the corresponding function of the DB object and return its results, you need to use the stored results. The following functions are added:

Listing 2: Extending the Dbquery class with a proxy.

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

http://www.bkjia.com/PHPjc/508643.html www.bkjia.com true http://www.bkjia.com/PHPjc/508643.html techarticle Dbquery Object Now, our Dbquery object simply mimics a stored procedure-once executed, returns a result resource that must be saved, and if you want to use the ...

  • 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.