How to track variable changes in PHP

Good afternoon.

I worked on a project that contains many variables and sessions and where most of the work is done β€œunder the hood” and through ajax.

The problem is that I am trying to debug my project, and I just cannot find a way to track and record the changes made to a specific variable.

I try to use firephp and xdebug, but they are not displayed when changes were made to the variable, but only its final value.

Any solution?

+4
source share
2 answers

Maybe a registered decorator can help you?

If you want to track some instance variables, you can wrap them with a decorator that implements the same interface. And in decorator methods, you can write a debugging level log, and then translate the workflow to the original variables stored as the field of the decorator object.

+1
source

XDebug can track changes in variables, just enable xdebug.collect_assignments and xdebug.collect_params , so when you create a trace log file, you should see the changes.

Configuration Example:

 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 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. 

Then, before or after assigning the variable for the first time, start tracking the code by adding this to your PHP code:

 xdebug_start_trace(); 

then add xdebug_stop_trace(); to the end, where do you think this has already happened.

Then check the file generated in the configured directory (specified by xdebug.trace_output_dir ). If the file is large, filter it by a specific variable using grep , for example.

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

or filter it in a smaller file: grep pattern trace-log.xt > smaller.log.txt .


Another alternative would be to use phpdbg .

+1
source

Source: https://habr.com/ru/post/1485206/


All Articles