调试应用程序(Debugging Applications)¶


Phalcon 调试应用程序_异常信息


Phalcon中提供了提供了几种调试级别即通知,错误和异常。 异常类  Phalcon主要使用这个异常类来包装这些功能,以方便开发人员或用户使用。

虽然Phalcon是使用C语言书写的。但其依旧如其他的PHP框架般提供了一些必须的调试工具。PHP本身提供的调试工具亦然能够正常使用。



捕获异常(Catching Exceptions)¶

纵观Phalcon的文档及其提供的样例程序。 有一个最直接的捕获异常的方法即是使用try/catch块:




<?php

try {

//... some phalcon/php code

} catch(\Exception $e) {

}


try/catch块内发生的异常会被捕获然后放入 $e 变量。 开发人员能够使用 Phalcon\Exception ( 来确定异常信息是来自Phalcon还是PHP本身。 PHP产生的全部异常都是基于 ​  。 且至少含有例如以下元素:




<?php

class Exception
{

/* Properties */
protected string $message;
protected int $code;
protected string $file;
protected int $line;

/* Methods */
public __construct ([ string $message = "" [, int $code = 0 [, Exception $previous = NULL ]]])
final public string getMessage ( void )
final public Exception getPrevious ( void )
final public mixed getCode ( void )
final public string getFile ( void )
final public int getLine ( void )
final public array getTrace ( void )
final public string getTraceAsString ( void )
public string __toString ( void )
final private void __clone ( void )

}


如从 ​ 中取得异常信息一样。 我也能够从 Phalcon\Exception 中取异常信息。




<?php

try {

//... app code ...

} catch(\Exception $e) {
echo get_class($e), ": ", $e->getMessage(), "\n";
echo " File=", $e->getFile(), "\n";
echo " Line=", $e->getLine(), "\n";
echo $e->getTraceAsString();
}


因此。 开发人员能够很easy找出在哪个文件里哪一行产生的异常信息,也能够看到异常信息涉及到的模块:




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(74):
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(83):
Phalcon\Mvc\Model::findFirst('email='demo@pha...')
#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}


从上面我们能够看到异常信息中有Phalcon的类及方法,甚至调用时的參数也显示了出来。 假设须要能够使用 ​  获取很多其它的信息。



调试组件(Debug component)¶

Phalcon提供的调试组件能够使开发人员更easy的定位代码中的错误。

以下的视频中展示了怎样工作的(因为CSDN的编辑器不支持iframe故无法播放):


要打开调试功能仅仅须要例如以下做:




<?php

$debug = new \Phalcon\Debug();
$debug->listen();


注意一定要去掉Try/Catch块才干够, 否则异常信息不会正确的输出(其实非常多时候Phalco显示异常信息是非常耗时的)。



反射与内省(Reflection and Introspection)¶

Phalcon的类的实例通常比一般的类实例更复杂。 这里我们能够使用 ‘Reflection API’_ (反射机制) 或 直接打印其内部状态来观其到底:




<?php

$router = new Phalcon\Mvc\Router();
print_r($router);


开发人员能够很easy的观察到对象内部的状态。

上面的代码输出例如以下:




Phalcon\Mvc\Router Object
(
[_dependencyInjector:protected] =>
[_module:protected] =>
[_controller:protected] =>
[_action:protected] =>
[_params:protected] => Array
(
)
[_routes:protected] => Array
(
[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\_]+)(/.*)*$#
[_compiledPattern: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:protected] =>
[_wasMatched:protected] =>
[_defaultModule:protected] =>
[_defaultController:protected] =>
[_defaultAction:protected] =>
[_defaultParams:protected] => Array
(
)
)



使用 XDebug(Using XDebug)¶

 是一个很好(奇妙)的调试工具,其和PHP内部调试工具一起为我们提调试工具(能够和PHP内置的调试工具互补)。

 也是一个PHP扩展, 所以二者能够一起使用。并且不须要额外的配置。

以下的视频中展示了Phalcon中使用XDebug的情况(因为CSDN的编辑器不支持iframe故无法播放):



一旦安装了xdebug, 开发人员便能够使用其API来取得很多其他的异常信息和其他信息:

我们强烈推荐至少使用XDebug 2.2.3 以提供与Phalcon最佳的兼容性。以下的样例中调用了 ​ 方法, 并返回了该方法产生的代码跟踪结果.




<?php

class SignupController extends \Phalcon\Mvc\Controller
{

public function indexAction()
{

}

public function registerAction()
{

// 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();
}

}


这个样例中, XDebug显示出了局部变量和代码的跟踪信息:




Xdebug: stop here! in /Applications/MAMP/htdocs/tutorial/app/controllers/SignupController.php
on line 19

Call 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


使用xdebug我们能够使用若干种方法来取得关phalcon应用的调试信息。很多其它信能够參 。