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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

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

Today I would like to introduce to you an article published by MIT last week, using GPT-3.5-turbo to solve the problem of time series anomaly detection, and initially verifying the effectiveness of LLM in time series anomaly detection. There is no finetune in the whole process, and GPT-3.5-turbo is used directly for anomaly detection. The core of this article is how to convert time series into input that can be recognized by GPT-3.5-turbo, and how to design prompts or pipelines to let LLM solve the anomaly detection task. Let me introduce this work to you in detail. Image paper title: Largelanguagemodelscanbezero-shotanomalydete

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

Exception handling and unit testing are important practices to ensure the soundness of C++ code. Exceptions are handled through try-catch blocks, and when the code throws an exception, it jumps to the catch block. Unit testing isolates code testing to verify that exception handling works as expected under different circumstances. Practical case: The sumArray function calculates the sum of array elements and throws an exception to handle an empty input array. Unit testing verifies the expected behavior of a function under abnormal circumstances, such as throwing an std::invalid_argument exception when an array is empty. Conclusion: By leveraging exception handling and unit testing, we can handle exceptions, prevent code from crashing, and ensure that the code behaves as expected under abnormal conditions.

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

Exception handling in C++ can be enhanced through custom exception classes that provide specific error messages, contextual information, and perform custom actions based on the error type. Define an exception class inherited from std::exception to provide specific error information. Use the throw keyword to throw a custom exception. Use dynamic_cast in a try-catch block to convert the caught exception to a custom exception type. In the actual case, the open_file function throws a FileNotFoundException exception. Catching and handling the exception can provide a more specific error message.

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

Nested exception handling is implemented in C++ through nested try-catch blocks, allowing new exceptions to be raised within the exception handler. The nested try-catch steps are as follows: 1. The outer try-catch block handles all exceptions, including those thrown by the inner exception handler. 2. The inner try-catch block handles specific types of exceptions, and if an out-of-scope exception occurs, control is given to the external exception handler.

Collection of C++ programming puzzles: stimulate thinking and improve programming skills Collection of C++ programming puzzles: stimulate thinking and improve programming skills Jun 01, 2024 pm 10:26 PM

C++ programming puzzles cover algorithm and data structure concepts such as Fibonacci sequence, factorial, Hamming distance, maximum and minimum values ​​of arrays, etc. By solving these puzzles, you can consolidate C++ knowledge and improve algorithm understanding and programming skills.

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

Pythonempowersbeginnersinproblem-solving.Itsuser-friendlysyntax,extensivelibrary,andfeaturessuchasvariables,conditionalstatements,andloopsenableefficientcodedevelopment.Frommanagingdatatocontrollingprogramflowandperformingrepetitivetasks,Pythonprovid

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

Python is an ideal programming introduction language for beginners through its ease of learning and powerful features. Its basics include: Variables: used to store data (numbers, strings, lists, etc.). Data type: Defines the type of data in the variable (integer, floating point, etc.). Operators: used for mathematical operations and comparisons. Control flow: Control the flow of code execution (conditional statements, loops).

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

Go functions are available as methods of objects. Methods are functions associated with an object that provide access to the object's fields and methods. In Go, methods are defined using func(receiver_type)identifier(parameters)return_type syntax. This approach plays an important role in object-oriented programming by providing encapsulation, reuse, and extensibility.

See all articles