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

PHP5 OOP programming proxy and custom exception_PHP tutorial

Jul 13, 2016 pm 05:33 PM
oop php5 one and acting land custom made object abnormal us imitate Now of Simple programming

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

Hot Article

Hot Article

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MIT's latest masterpiece: using GPT-3.5 to solve the problem of time series anomaly detection MIT's latest masterpiece: using GPT-3.5 to solve the problem of time series anomaly detection Jun 08, 2024 pm 06:09 PM

MIT's latest masterpiece: using GPT-3.5 to solve the problem of time series anomaly detection

C++ function exceptions and single testing: ensuring code soundness C++ function exceptions and single testing: ensuring code soundness May 03, 2024 am 09:18 AM

C++ function exceptions and single testing: ensuring code soundness

How to implement nested exception handling in C++? How to implement nested exception handling in C++? Jun 05, 2024 pm 09:15 PM

How to implement nested exception handling in C++?

C++ Function Exception Advanced: Customized Error Handling C++ Function Exception Advanced: Customized Error Handling May 01, 2024 pm 06:39 PM

C++ Function Exception Advanced: Customized Error Handling

Application of Golang functions in object-oriented programming Application of Golang functions in object-oriented programming May 31, 2024 pm 07:36 PM

Application of Golang functions in object-oriented programming

The Key to Coding: Unlocking the Power of Python for Beginners The Key to Coding: Unlocking the Power of Python for Beginners Oct 11, 2024 pm 12:17 PM

The Key to Coding: Unlocking the Power of Python for Beginners

Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder Oct 11, 2024 pm 08:58 PM

Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder

Java Made Simple: A Beginner's Guide to Programming Power Java Made Simple: A Beginner's Guide to Programming Power Oct 11, 2024 pm 06:30 PM

Java Made Simple: A Beginner's Guide to Programming Power

See all articles