PHP面向对象编程中的代理与异常定制(1)
一、 DBQuery对象
现在,我们的DBQuery对象简单地模仿一个存储过程—一旦被执行,即返回一个必须进行保存的结果资源;并且如果你想使用该结果集上的函数(例如num_rows()或fetch_row())的话,你必须传递MySqlDB对象。那么,如果由DBQuery对象来实现MySqlDB对象(其设计目的是对一个执行查询的结果进行操作)实现的函数,效果如何呢?让我们继续使用上一篇示例中的代码;并且让我们假定,现在由DBQuery对象管理我们的结果资源。DBQuery类的源码如列表1所示。
列表1.使用DBQuery类。
<ol class="dp-c"> <li class="alt"><span><span class="keyword">require</span><span> </span><span class="string">'mysql_db.php'</span><span>; </span></span></li> <li> <span class="keyword">require_once</span><span> </span><span class="string">'query.php'</span><span>; </span> </li> <li class="alt"> <span class="vars">$db</span><span> = </span><span class="keyword">new</span><span> MySqlDb; </span> </li> <li> <span class="vars">$db</span><span>->connect(</span><span class="string">'host'</span><span>, </span><span class="string">'username'</span><span>, </span><span class="string">'pass'</span><span>); </span> </li> <li class="alt"> <span class="vars">$db</span><span>->query(</span><span class="string">'use content_management_system'</span><span>); </span> </li> <li> <span class="vars">$query</span><span> = </span><span class="keyword">new</span><span> DBQuery(</span><span class="vars">$db</span><span>); </span> </li> <li class="alt"> <span class="vars">$query</span><span>->prepare(</span><span class="string">'SELECT fname,sname FROM users WHERE username=:1S AND pword=:2S AND expire_time<:3i><span>); </span><li><span>try { </span></li> <li class="alt"> <span> </span><span class="keyword">if</span><span>(</span><span class="vars">$query</span><span>->execute(</span><span class="string">"visualad"</span><span>, </span><span class="string">"apron"</span><span>, time()))->num_rows() == 1) { </span> </li> <li> <span></span><span class="func">echo</span><span>(</span><span class="string">'Correct Credentials'</span><span>); </span> </li> <li class="alt"> <span> } </span><span class="keyword">else</span><span> { </span> </li> <li> <span></span><span class="func">echo</span><span>(</span><span class="string">'Incorrect Credentials / Session Expired'</span><span>); </span> </li> <li class="alt"><span> } </span></li> <li> <span>} catch (QueryException </span><span class="vars">$e</span><span>) { </span> </li> <li class="alt"> <span> </span><span class="func">echo</span><span>(</span><span class="string">'Error executing query: '</span><span> . </span><span class="vars">$e</span><span>); </span> </li> <li><span>} </span></li></:3i></span> </li> </ol>
上面修改后的代码中我们最感兴趣的是,catch语句和execute语句。
◆execute语句不再返回一个结果资源,现在它返回DBQuery对象本身。
◆DBQuery对象现在实现num_rows()函数—我们从DB接口中已经熟悉。
◆如果查询执行失败,它抛出一个QueryException类型的异常。当被转换成一个字符串时,它将返回发生的错误的细节信息。
为此,你需要使用PHP代理。事实上,你在我们的DBQuery对象中已经使用代理了,但是现在将更为深入地使用它来把它与MySqlDB对象紧密绑定。该DBQuery对象已经被使用一个实现DB接口的对象初始化,并且它已经包含一个成员函数execute—由它调用DB对象的query()方法来执行该查询。这个DBQuery对象本身并不实际地查询数据库,它把这项任务交由DB对象来完成。这就是代理,其实是一个进程—借助于这个进程,通过把消息发送给另一个实现相同的或类似行为的对象,一个对象可以实现一个特别的行为。
为此,你需要修改DBQuery对象以便包括所有的函数—它们操作一个来自DB对象的结果资源。当执行查询以调用DB对象的相应函数并且返回它的结果时,你需要使用存储的结果。下列函数将被添加:
列表2:使用代理扩展DBQuery类。
<ol class="dp-c"> <li class="alt"><span><span class="keyword">class</span><span> DBQuery </span></span></li> <li><span>{ </span></li> <li class="alt"><span> ..... </span></li> <li><span> </span></li> <li class="alt"> <span> </span><span class="keyword">public</span><span> </span><span class="keyword">function</span><span> fetch_array() </span> </li> <li><span> { </span></li> <li class="alt"> <span></span><span class="keyword">if</span><span> (! </span><span class="func">is_resource</span><span>(</span><span class="vars">$this</span><span>->result)) { </span> </li> <li> <span> </span><span class="keyword">throw</span><span> </span><span class="keyword">new</span><span> Exception(</span><span class="string">'Query not executed.'</span><span>); </span> </li> <li class="alt"><span>} </span></li> <li><span> </span></li> <li class="alt"> <span></span><span class="keyword">return</span><span> </span><span class="vars">$this</span><span>->db->fetch_array(</span><span class="vars">$this</span><span>->result); </span> </li> <li><span> } </span></li> <li class="alt"><span> </span></li> <li> <span> </span><span class="keyword">public</span><span> </span><span class="keyword">function</span><span> fetch_row() </span> </li> <li class="alt"><span> { </span></li> <li> <span></span><span class="keyword">if</span><span> (! </span><span class="func">is_resource</span><span>(</span><span class="vars">$this</span><span>->result)) { </span> </li> <li class="alt"> <span> </span><span class="keyword">throw</span><span> </span><span class="keyword">new</span><span> Exception(</span><span class="string">'Query not executed.'</span><span>); </span> </li> <li><span>} </span></li> <li class="alt"><span> </span></li> <li> <span></span><span class="keyword">return</span><span> </span><span class="vars">$this</span><span>->db->fetch_row(</span><span class="vars">$this</span><span>->result); </span> </li> <li class="alt"><span> } </span></li> <li><span> </span></li> <li class="alt"> <span> </span><span class="keyword">public</span><span> </span><span class="keyword">function</span><span> fetch_assoc() </span> </li> <li><span> { </span></li> <li class="alt"> <span></span><span class="keyword">if</span><span> (! </span><span class="func">is_resource</span><span>(</span><span class="vars">$this</span><span>->result)) { </span> </li> <li> <span> </span><span class="keyword">throw</span><span> </span><span class="keyword">new</span><span> Exception(</span><span class="string">'Query not executed.'</span><span>); </span> </li> <li class="alt"><span>} </span></li> <li><span> </span></li> <li class="alt"> <span></span><span class="keyword">return</span><span> </span><span class="vars">$this</span><span>->db->fetch_assoc(</span><span class="vars">$this</span><span>->result); </span> </li> <li><span> } </span></li> <li class="alt"><span> </span></li> <li> <span> </span><span class="keyword">public</span><span> </span><span class="keyword">function</span><span> fetch_object() </span> </li> <li class="alt"><span> { </span></li> <li> <span></span><span class="keyword">if</span><span> (! </span><span class="func">is_resource</span><span>(</span><span class="vars">$this</span><span>->result)) { </span> </li> <li class="alt"> <span> </span><span class="keyword">throw</span><span> </span><span class="keyword">new</span><span> Exception(</span><span class="string">'Query not executed.'</span><span>); </span> </li> <li><span>} </span></li> <li class="alt"><span> </span></li> <li> <span></span><span class="keyword">return</span><span> </span><span class="vars">$this</span><span>->db->fetch_object(</span><span class="vars">$this</span><span>->result); </span> </li> <li class="alt"><span> } </span></li> <li><span> </span></li> <li class="alt"> <span> </span><span class="keyword">public</span><span> </span><span class="keyword">function</span><span> num_rows() </span> </li> <li><span> { </span></li> <li class="alt"> <span></span><span class="keyword">if</span><span> (! </span><span class="func">is_resource</span><span>(</span><span class="vars">$this</span><span>->result)) { </span> </li> <li> <span> </span><span class="keyword">throw</span><span> </span><span class="keyword">new</span><span> Exception(</span><span class="string">'Query not executed.'</span><span>); </span> </li> <li class="alt"><span>} </span></li> <li><span> </span></li> <li class="alt"> <span></span><span class="keyword">return</span><span> </span><span class="vars">$this</span><span>->db->num_rows(</span><span class="vars">$this</span><span>->result); </span> </li> <li><span> } </span></li> <li class="alt"><span>} </span></li> </ol>
每个函数的实现相当简单。它首先进行检查,以确保已经执行查询,然后把任务代理到DB对象,返回它的结果就好象它是查询对象本身(称作是基本数据库函数)一样。 1

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

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

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

C is an ideal choice for beginners to learn system programming. It contains the following components: header files, functions and main functions. A simple C program that can print "HelloWorld" needs a header file containing the standard input/output function declaration and uses the printf function in the main function to print. C programs can be compiled and run by using the GCC compiler. After you master the basics, you can move on to topics such as data types, functions, arrays, and file handling to become a proficient C programmer.

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.
