【问题标题】:How to track variable changes in PHP如何在 PHP 中跟踪变量变化
【发布时间】:2013-06-04 20:39:11
【问题描述】:

美好的一天。

我一直在从事包含许多变量和会话的项目,其中大部分工作都是在“幕后”和通过 ajax 完成的。

问题是我尝试调试我的项目,但我找不到任何方法来跟踪和记录对某个变量所做的更改。

我一直在尝试使用 firephp 和 xdebug,但它们不显示何时更改变量,只显示其最终值。

有什么办法吗?

【问题讨论】:

  • 我明白你想做什么,但是为什么,是什么原因?
  • 如果您想要手动操作,那么在每次更改变量时,您都无法将变量值分配给数组,例如 $logs[]=$variable;,但您最终只需在每次更改时执行 dump($logs)
  • 在课堂上,你可以使用__set
  • 调试中,我确定最终结果是错误的。我已经尝试了几天一步一步地遵循它(相信我有很多步骤)但徒劳无功。我希望这个日志能让我发现问题的原因。
  • @Recode:除非您设置的属性未在类上声明,否则您不能这样做。不声明属性是一种非常糟糕的编码方式。当然,如果保存对象的变量发生变化,只有当对象的属性发生变化时,它也无济于事。

标签: php debugging xdebug firephp


【解决方案1】:

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将其过滤到较小的文件中。


另一种选择是使用phpdbg

【讨论】:

    【解决方案2】:

    可以登录decorator可以帮到你吗?

    如果你想跟踪一些实例变量,你可以用实现相同接口的装饰器将它们包裹起来。在装饰器方法中,您可以编写调试级别日志,然后将工作流委托给保存为装饰器对象字段的原始变量。

    【讨论】:

      猜你喜欢
      • 2015-12-07
      • 1970-01-01
      • 1970-01-01
      • 2011-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多