Debug application (Debugging applications)

Source: Internet
Author: User

Debug application (Debugging applications) ¶

Phalcon provides several debug levels that are notifications, errors, and exceptions. The exception class Exception class provides some common debugging information such as files, lines, messages, error codes, trace information, and so on when errors occur. Phalcon mainly uses this exception class to wrap these features to make it easier for developers or users to use.

Although Phalcon is written in C, it still provides some necessary debugging tools like other PHP frameworks, and the debugging tools provided by PHP itself can be used normally.

Catch exception (catching Exceptions) ¶

Throughout Phalcon's documentation and the example programs it provides, one of the most straightforward ways to catch exceptions is to use Try/catch blocks:

<?php{    //... Some phalcon/php Code($e{}     

Exceptions that occur within a try/catch block are captured and then placed into $e variables. Developers can use phalcon\exception (extended from Exception Class) to determine whether the exception information is from Phalcon or PHP itself. All exceptions generated by PHP are based on the Exception class and contain at least the following elements:

<?phpClassException{/* Properties */ProtectedString$message;ProtectedInt$code;ProtectedString$file;ProtectedInt$line;/* Methods */Public__construct([String$message=""[,Int$code=0[,Exception$previous=Null]]])FinalPublicStringGetMessage(void)FinalPublicExceptionGetPrevious(void)FinalPublicMixedGetCode(void)FinalPublicStringGetFile(void)Finalpublic int getline  (void ) final public array gettrace  (void ) final public string gettraceasstring  (void  ) public string __tostring  ( void ) final private void __clone  (void ) }        

I can also take exception information from the phalcon\exception , like getting exception information from Exception class.

<?phpTry{// ... APP code ...}Catch(\exception$e){EchoGet_class($e),": ", $e ->getmessage (),  \n "echo  "file="  $e -> getfile (),  "\n" echo  "line="  $e -> getline (),  "\n" echo  $e ->gettraceasstring ( );                /span>                

As a result, developers can easily find out which of the files in which the exception information is generated, as well as the modules that are involved in the exception information:

PDOEXCEPTION:SQLSTATE[28000] [1045] Access denied for user ' root ' @ ' localhost ' (using Password:no) file=/applications/ mamp/htdocs/invo/public/index.php line=74#0 [Internal function]: pdo->__construct (' Mysql:host=loca ... ', ' root ', ' , array) #1 [internal function]: Phalcon\db\adapter\pdo->connect (array) #2/applications/mamp/htdocs/invo/public/ index.php: Phalcon\db\adapter\pdo->__construct (Array) #3 [internal function]: {closure} () #4 [internal function ]: Call_user_func_array (Object (Closure), array) #5 [internal function]: Phalcon\di->_factory (Object (Closure), Array) #6 [internal function]: phalcon\di->get (' db ', Array) #7 [internal function]: phalcon\di->getshared (' db ') #8 [Internal function]: phalcon\mvc\model->getconnection () #9 [internal function]: phalcon\mvc\model::_ Getorcreateresultset (' Users ', Array, true) #10/applications/mamp/htdocs/invo/app/controllers/ sessioncontroller.php: Phalcon\mvc\model::findfirst (' email= ' [email protected] ') #11 [internaL Function]: sessioncontroller->startaction () #12 [internal function]: Call_user_func_array (array, array) #13 [ Internal function]: Phalcon\mvc\dispatcher->dispatch () #14/applications/mamp/htdocs/invo/public/index.php (114) : Phalcon\mvc\application->handle () #15 {main}

From the above we can see that there are Phalcon classes and methods in the exception information, even when the parameters of the call are displayed. If necessary, you can use Exception::gettrace to get more information.

Debug components (Debug component) ¶

The debug component provided by Phalcon makes it easier for developers to locate errors in the code.

The following video shows how it works:

To turn on the debug feature, you only need to do the following:

<?php\phalcon\debug();  $debug-Listen();     

Note that you must remove the Try/catch block to be able, otherwise the exception information will not be the correct output (in fact, many times Phalco display exception information is very time-consuming).

Reflection and introspection (Reflection and introspection) ¶

Instances of Phalcon classes are often more complex than generic class instances. Here we can use the ' Reflection API ' _ (reflection mechanism) or directly print its internal state to view it:

<?phpphalcon\mvc\router();  Print_r($router);     

Developers can easily observe the state inside the object. The above code output is as follows:

Phalcon\mvc\router Object ([_dependencyinjector:protected] = [_module:protected] = [_controller:protected ] = [_action:protected] = [_params:protected] = Array () [_routes:protected] = ARR Ay ([0] = = Phalcon\mvc\router\route Object ([_pattern:protected]                    = = #^/([a-za-z0-9\_]+) [/]{0,1}$# [_compiledpattern:protected] = #^/([a-za-z0-9\_]+] [/]{0,1}$#                        [_paths:protected] = = Array ([Controller] = 1                    ) [_methods:protected] = [_id:protected] = 0                    [_name:protected] = [1] = Phalcon\mvc\router\route Object ( [_pattern:protected] = #^/([a-za-z0-9\_]+)/([a-za-z0-9\_]+) (/.*) *$# [_compiledpat Tern:protected] = #^/([a-za-z0-9\_]+)/([a-za-z0-9\_]+) (/.*) *$# [_paths:protected] = = Array ( [Controller] = 1 [Action] = 2 [params ] = 3) [_methods:protected] = [_id:protected] = = 1 [_name:protected] =) [_matchedroute:protected] = [_matches:pro     Tected] = [_wasmatched:protected] = [_defaultmodule:protected] [_defaultcontroller:protected] = = [_defaultaction:protected] = [_defaultparams:protected] = = Array ())
Using XDebug (using XDebug) ¶

XDebug is a very good (magical) debugging tool that provides debugging tools (complementary to PHP's built-in debugging tools) together with the PHP internal debugging tools. XDebug is also a php extension, so they can be used together, and no additional configuration is required.

The following video shows the usage of xdebug in Phalcon:

Once the Xdebug is installed, developers can use their APIs to get more exception information and other information:

we strongly recommend using at least Xdebug 2.2.3 to provide the best compatibility with Phalcon.

The following example calls the Xdebug_print_function_stack method and returns the code trace results generated by the method:

<?phpUsePhalcon\mvc\controller;ClassSignupcontrollerExtendsController{PublicfunctionIndexaction(){}PublicfunctionRegisteraction(){Request variables from HTML form$name=$this-Request-Getpost("Name","String");$email=$this-Request-Getpost("Email" "email" //Stop execution and show a backtrace return xdebug_print_function_ Stack ( "Stop here!" );  $user = new users ();  $user ->name =  $name  $user ->email =  $email //Store and check for errors  $user ->save (); }}             /span>                

In this example, Xdebug shows the tracking information for the local variables and the code:

Xdebug:stop here! in/applications/mamp/htdocs/tutorial/app/controllers/signupcontroller.php on line    19Call Stack:    0.0383     654600   1. {main} ()/applications/mamp/htdocs/tutorial/public/index.php:0    0.0392     663864   2. Phalcon\mvc\application->handle ()        /applications/mamp/htdocs/tutorial/public/index.php:37    0.0418     738848   3. Signupcontroller->registeraction ()        /applications/mamp/htdocs/tutorial/public/index.php:0    0.0419     740144   4. Xdebug_print_function_stack ()        /applications/mamp/htdocs/tutorial/app/controllers/signupcontroller.php : 19

Using Xdebug we can use several methods to obtain debug information about the Phalcon application. More letters can be found here in XDebug documentation (XDEBUG documentation).

Debug application (Debugging applications)

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.