2012-01-03 27 views

Trả lời

7

get_defined_vars() sẽ trả về một mảng của tất cả các biến được xác định. Sau đó bạn có thể lặp qua từng cái một và đổ các nội dung, hoặc sử dụng var_dump(get_defined_vars()) để đổ chúng vào.

+2

Đúng vậy, tuy nhiên bạn nên lưu ý rằng đây sẽ không chính xác là mã xử lý ngoại lệ. Bạn sẽ phải được resopnsible để cung cấp những ngoại lệ trong khi ném ngoại lệ. Điều này đang được nói, tôi không biết bất kỳ cách nào bạn có thể thực hiện tương tự trong việc xử lý ngoại lệ. – Slavic

2

Hãy thử var_dump(get_defined_vars());. Điều đó sẽ đổ tất cả các biến được xác định trong phạm vi.

0

Bạn có thể có tất cả được xác định (bao gồm cả được xác định trước) bằng cách gọi hàm get_defined_vars.

Chỉ các biến cục bộ (phạm vi chức năng hiện tại) không có sẵn mà không có một số công cụ gỡ lỗi.

2

Tôi nghĩ tốt hơn là thiết lập xDebug.

Nó tự động xử lý tất cả môi trường.

Và bạn sẽ nhận được mọi cái gì đó như:

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* 

Nó làm vô cùng dễ dàng gỡ lỗi.

2

Trình xử lý ngoại lệ sẽ không bao giờ có quyền truy cập vào các biến cục bộ của hàm mà ngoại lệ được nêu ra vì các biến đó là cục bộ. Tôi không nghĩ rằng có một cách để có được quyền truy cập vào bảng biến của một khung ngăn xếp cụ thể từ mã PHP của người dùng.

Bạn có thể thực hiện việc này bằng cách sử dụng xdebug làm trình gỡ rối bước. Với các điểm ngắt (ví dụ: trong trình xử lý ngoại lệ), bạn có thể xem xét từng khung ngăn xếp.

Tiếp bước gỡ lỗi, cũng có một số tùy chọn hiển thị bạn có thể muốn sử dụng, như:

2

Bạn có thể truy cập đến các biến địa phương, nếu ngoại lệ sẽ được bị bắt trong phạm vi/chức năng tương tự, nó được ném:

<?php 

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

?> 
Các vấn đề liên quan