In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Today, I will talk to you about how to carry out PHP object-oriented programming agents and abnormal customization, many people may not understand, in order to make you better understand, the editor summed up the following content, I hope you can get something according to this article.
1. DBQuery object
Now, our DBQuery object simply mimics a stored procedure-once executed, it returns a result resource that must be saved; and if you want to use a function on the result set (such as num_rows () or fetch_row ()), you must pass the MySqlDB object. So what if the DBQuery object implements a function implemented by the MySqlDB object, which is designed to manipulate a result of executing a query? Our result resources are now managed by the DBQuery object. The source code for the DBQuery class is shown in listing 1.
Listing 1. Use the DBQuery class.
Require 'mysql_db.php'; require_once' query.php'; $db = new MySqlDb; $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_timestored_procedure) {/ * call the compile function to get the query * / $query = call_user_func_array (array ($this,' compile'), $args);} else {/ * A stored procedure is not initialized, so * / $query = $queryParams;} $result = $this- > db- > query ($query) executed as a standard query If (! $result) {throw new QueryException ($this);} $this- > result = $result; / * Note how we now return the object itself, which enables us to call the member function * / return $this;} from the return result of this function.
Fourth, use inheritance to throw custom exceptions
In PHP, you can throw any object as a PHP exception; but first, the exception should inherit from PHP's built-in exception class. By creating your own custom exception, you can record other information about the error, such as creating an entry in a log file, or doing whatever you like. Your custom exception will do the following:
◆ logs error messages from the DB object generated by the query.
◆ gives the exact details of the line of code on which the query error occurred-by checking the call stack.
◆ displays error messages and query text-when converted to a string.
In order to get the error message and query text, you need to make several changes to the DBQuery object.
1. A new protected attribute, compiledQuery-, needs to be added to the class.
2. The compile () function updates the query compiledQuery property with the query text.
3. You should add a function to retrieve the compiled query text.
4. You should also add a function that gets the current DB object associated with the DBQuery object.
Listing 4. Throw a PHP exception.
Class DBQuery {/ * stores the compiled version of the query * * @ var string $compiledQuery * / protected $compiledQuery; / * after calling compile () or execute () and returns the compiled query without executing it. * @ parameter: mixed $params,... Query parameter * @ returned: string-compiled query * / public function compile ($params=') {if (! $this- > stored_procedure) {throw new Exception ("stored procedure was not initialized.");} / * instead of parameter * / $params= func_get_args () / / get the function parameter $query = preg_replace ("/ (? compile_callback ($params, 1," 2)', $this- > query); return ($this- > compiledQuery = $this- > add_strings ($query)); / / put the string back in the query} public function getDB () {return $this- > db;} public function getCompiledQuery () {return $this- > compiledQuery;}}
Now you can implement the QueryException class. Notice how you traverse the call stack to find the location in the script that actually caused the error. This applies when the DBQuery object that throws the exception is a subclass that inherits from the DBQuery object.
List the 5:QueryException class.
/ * * query exception * * when attempting to execute a query, if an error occurs, the error * / class QueryException extends Exception {/ * query text * * @ var string $QueryText; * / protected $QueryText; / * * from the database * * @ var string $ErrorCode * / protected $ErrorNumber will be thrown by the {@ query} object / * error message from database * * @ var string $ErrorMessage * / protected $ErrorMessage; / * * Class constructor * * @ parameter: DBQuery $db, is the query object that throws an exception * / public function _ _ construct (DBQuery $query) {/ * get call stack * / $backtrace = $this- > GetTrace () / * set the lines and files to the location where the error actually occurred * / if (count ($backtrace) > 0) {$x = 1; / * if the query class is inherited, then we need to ignore the calls made by the subclass * / while ((! Isset ($backtrace [$x] ['line'])) | (isset ($backtrace [$x] [' class']) & & is_subclass_of ($backtrace [$x] ['class'],' DBQuery')) | (strpos (strtolower (@ $backtrace [$x] ['function']),' call_user_func'))! = false) {/ * cycle execution, as long as the function without a line number or called is a subclass of class DBQuery * / + $x / * if we reach the bottom of the stack, we use * callers * / if (($x) > = count ($backtrace)) {$x = count ($backtrace); break;}} / * if the above loop is executed at least once, then we can subtract it by 1 to find the actual line of code that caused the error * / if ($x! = 1) {$x-= 1 } / *, we can set the file and line number, which should reflect the SQL statement * / $this- > line = $backtrace [$x] ['line']; $this- > file = $backtrace [$x] [' file'];} $this- > QueryText = $query- > getCompiledQuery (); $this- > ErrorNumber = $query- > getDB ()-> errno (); $this- > ErrorMessage = $query- > getDB ()-> error () / * call the exception constructor of the superclass * / parent::__construct ('Query Error', 0);} / * get the query text * * @ return string query text * / public function GetQueryText () {return $this- > QueryText } / * * get the error number * * @ return string error number * / public function GetErrorNumber () {return $this- > ErrorNumber;} / * * get the error message * * @ return string error message * / public function GetErrorMessage () {return $this- > ErrorMessage;} / * * called when the object is converted to a string. * @ returns the string * / public function _ toString () {$output = "Query Error in {$this- > file} on line {$this- > line} nn"; $output. = "Query: {$this- > QueryText} n"; $output. = "Error: {$this- > ErrorMessage} ({$this- > ErrorNumber}) nn"; return $output;}}
At this point, the code you see at the beginning of this section is ready to work.
You see how the PHP agent maps the DB interface associated with a query to an operation on a particular query result. The DBQuery object exposes the same function, such as fetch_assoc (), as a DB object. However, these all work for a single query. You also learned how to use custom exceptions to give details-when and where an error occurred, and how they better control error handling.
After reading the above, do you have any further understanding of how to do proxy and exception customization in PHP object-oriented programming? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.