簡體   English   中英

如何跟蹤PHP中的變量更改

[英]How to track variable changes in PHP

美好的一天。

我一直在從事包含許多變量和會話的項目,其中大部分工作是在“幕后”和通過Ajax完成的。

問題是我嘗試調試我的項目,但找不到任何跟蹤和記錄對某個變量所做的更改的方法。

我一直在嘗試使用firephp和xdebug,但它們不顯示何時對變量進行更改,僅顯示其最終值。

有什么辦法嗎?

可以登錄的裝飾器可以幫助您嗎?

如果要跟蹤某些實例變量,則可以使用實現相同接口的裝飾器將它們包裝起來。 在裝飾器方法中,您可以編寫調試級別日志,然后將工作流與保存為裝飾器對象字段的原始變量進行綁定。

XDebug可以跟蹤變量更改,只需啟用xdebug.collect_assignmentsxdebug.collect_params ,因此,在生成跟蹤日志文件時,您應該看到更改。

配置示例:

xdebug.default_enable = 1           ; bool: The stacktraces will be shown by default on an error event.
xdebug.collect_vars = 1             ; bool: Gather information about which variables are used in a certain scope.
xdebug.show_local_vars=1            ; int: Generate stack dumps in error situations.
xdebug.collect_assignments=1        ; bool: Controls whether Xdebug should add variable assignments to function traces.
xdebug.collect_params=4             ; int1-4: Collect the parameters passed to functions when a function call is recorded.
xdebug.collect_return=1             ; bool: Write the return value of function calls to the trace files.
xdebug.var_display_max_children=256 ; int: Amount of array children and object's properties are shown.
xdebug.var_display_max_data=1024    ; int: Max string length that is shown when variables are displayed.
xdebug.var_display_max_depth=5      ; int: How many nested levels of array/object elements are displayed.
xdebug.trace_output_dir="/var/log/xdebug" ; string: Directory where the tracing files will be written to.

然后在您第一次分配變量之前或之后,通過將其添加到您的PHP代碼中來開始跟蹤代碼:

xdebug_start_trace();

然后可選地添加xdebug_stop_trace(); 最后,您認為它已經發生了。

然后檢查在配置目錄(由xdebug.trace_output_dir指定)中生成的文件。 如果文件很大,請使用grep按特定變量過濾,例如

grep --color=auto variable trace-log.xt

或通過以下grep pattern trace-log.xt > smaller.log.txt將其過濾到較小的文件中: grep pattern trace-log.xt > smaller.log.txt


另一種選擇是使用phpdbg

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM