2012-01-03 19 views

回答

7

get_defined_vars()將返回所有已定義變量的數組。然後,您可以遍歷每個內容並轉儲內容,或使用var_dump(get_defined_vars())將其全部轉儲。

+2

這是正確的,但它是很好的注意,這也不會是完全的異常處理程序代碼。在拋出異常的時候,你必須被重新提供給異常。這就是說,我不知道你有什麼辦法可以在異常處理中做到這一點。 – Slavic 2012-01-03 13:08:20

2

嘗試var_dump(get_defined_vars());。這應該轉儲範圍內的所有定義的變量。

0

您可以通過調用函數get_defined_vars定義所有(包括預定義的)。

只有局部變量(當前函數的作用域)在沒有某些調試工具的情況下不可用。

2

我覺得更好的是設置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* 

這使得非常容易調試。

2

異常處理程序永遠不會訪問異常引發的函數的局部變量,因爲這些變量是局部變量。我不認爲有一種方法可以從userland PHP代碼訪問特定棧幀的變量表。

您可以使用xdebug作爲步調試器來完成此操作。通過斷點(例如,在異常處理程序中),您可以查看每個堆棧幀。

下一步步調試,也有你可能想利用一些顯示選項,如:

2

您可以訪問局部變量,如果異常會夾在同一範圍/職能是拋出:

<?php 

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

?> 
相關問題