Agent and customization exceptions in PHP5OOP programming _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Exceptions in proxy and customization of PHP5OOP programming. 1. DBQuery object 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 one, 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 MySQL (best combination with PHP) DB object. Then, if the DBQuery object is used to implement the MySQL (the best combination with PHP) DB object (which is designed to operate on a query execution result) function, what is the effect? 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 (the best combination with PHP) _ db. php (as the mainstream development language );
Require_once query. php (as the 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 =: 1 s and pword =: 2 s 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.

· The 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 a proxy. In fact, you have used a proxy in our DBQuery object, but now we will use it more deeply to closely bind it to the DB object of MySQL (the best combination with PHP. 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.

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.