PHP:如何在异常处理程序中转储所有局部变量


PHP: How to dump all local variables in an exception handler

我正在寻找一种方法,让我的异常处理程序转储异常引发的函数的局部变量。这可能吗?

get_defined_vars()将返回包含所有已定义变量的数组。然后,您可以循环遍历每个文件并转储内容,或者使用var_dump( get_defined_vars() )将它们全部转储。

试试var_dump( get_defined_vars( ) );

我认为最好是设置xDebug。

自动转储所有环境。

你每次都会得到这样的东西:

 Catchable fatal error: Argument 1 passed to RogoDeal::getDealerForMe() must be an instance of RogoParticipant, instance of myUser given in G:'webroot'v1-1-5.omyconf'lib'model'doctrine'RogoDeal.class.php on line 512
Call Stack:
    0.0002     336944   1. {main}() G:'webroot'v1-1-5.omyconf'web'frontend_dev.php:0
    0.1244    1983360   2. sfContext->dispatch() G:'webroot'v1-1-5.omyconf'web'frontend_dev.php:13
    0.1244    1983392   3. sfFrontWebController->dispatch() G:'webroot'symfony'lib'util'sfContext.class.php:170
    0.1248    1987104   4. sfController->forward() G:'webroot'symfony'lib'controller'sfFrontWebController.class.php:48
    0.1500    2085896   5. sfFilterChain->execute() G:'webroot'symfony'lib'controller'sfController.class.php:238
    0.1504    2086752   6. sfRenderingFilter->execute() G:'webroot'symfony'lib'filter'sfFilterChain.class.php:53
    0.1504    2086752   7. sfFilterChain->execute() G:'webroot'symfony'lib'filter'sfRenderingFilter.class.php:33
    0.1508    2087584   8. sfBasicSecurityFilter->execute() G:'webroot'symfony'lib'filter'sfFilterChain.class.php:53
    0.1512    2087584   9. sfFilterChain->execute() G:'webroot'symfony'lib'filter'sfBasicSecurityFilter.class.php:72
    0.1515    2088408  10. sfCacheFilter->execute() G:'webroot'symfony'lib'filter'sfFilterChain.class.php:53
    0.1549    2089920  11. sfFilterChain->execute() G:'webroot'symfony'lib'filter'sfCacheFilter.class.php:65
    0.1553    2090744  12. sfExecutionFilter->execute() G:'webroot'symfony'lib'filter'sfFilterChain.class.php:53
   14.8569   19778472  13. sfExecutionFilter->handleView() G:'webroot'symfony'lib'filter'sfExecutionFilter.class.php:47
   14.8570   19778472  14. sfExecutionFilter->executeView() G:'webroot'symfony'lib'filter'sfExecutionFilter.class.php:116
   14.8662   19806016  15. sfPHPView->render() G:'webroot'symfony'lib'filter'sfExecutionFilter.class.php:155
   14.8673   19806352  16. sfPHPView->renderFile() G:'webroot'symfony'lib'view'sfPHPView.class.php:185
   14.8801   19926728  17. require('G:'webroot'v1-1-5.omyconf'apps'frontend'modules'program'templates'markedSuccess.php') G:'webroot'symfony'lib'view'sfPHPView.class.php:75
   16.2403   21796104  18. sfOutputEscaperIteratorDecorator->getDealerForMe() G:'webroot'v1-1-5.omyconf'apps'frontend'modules'program'templates'markedSuccess.php:48
   16.2403   21796304  19. sfOutputEscaperObjectDecorator->__call() G:'webroot'v1-1-5.omyconf'apps'frontend'modules'program'templates'markedSuccess.php:48
   16.2404   21796552  20. call_user_func_array() G:'webroot'symfony'lib'escaper'sfOutputEscaperObjectDecorator.class.php:64
   16.2404   21796736  21. RogoDeal->getDealerForMe() G:'webroot'symfony'lib'escaper'sfOutputEscaperObjectDecorator.class.php:64
Dump $_SERVER
   $_SERVER['REQUEST_METHOD'] = 'GET'
   $_SERVER['REQUEST_URI'] = '/frontend_dev.php/program/marked?interface=mobile'
   $_SERVER['HTTP_USER_AGENT'] = 'Opera/9.80 (Windows NT 6.1; U; Edition Ukraine Local; ru) Presto/2.10.229 Version/11.60'
Dump $_REQUEST
   $_REQUEST['interface'] = 'mobile'
Variables in local scope (#21):
  $dealer = *uninitialized*
  $me = *uninitialized*
  $owner = *uninitialized*

异常处理程序将永远无法访问异常引发的函数的局部变量,因为这些变量是局部的。我不认为有一种方法可以从用户的PHP代码中获得对特定堆栈框架的变量表的访问权。

您可以通过使用xdebug作为阶跃调试器来实现这一点。使用断点(例如在异常处理程序中),您可以查看每个堆栈帧。

在步骤调试之后,还有一些您可能想要使用的显示选项,例如:

  • http://xdebug.org/docs/stack_trace add-local-vars

你可以访问局部变量,如果异常将在相同的作用域/函数中被捕获,它将被抛出:

<?php
try
{
    $var = 123;
    throw new Exception();
}
catch (Exception $e)
{
    var_dump($var); // int(123)
}
?>

可以通过调用函数get_defined_vars来定义(包括预定义)。

只有局部变量(当前函数的范围)是不可用的,没有一些调试工具