5

Good day.

I've been working on project that contains many variables and sessions and where most of the job is done "under the hood" and via ajax.

The problem is that I try to debug my project and I just can't find any way to track and log changes that're made to a certain variable.

I've been trying to use firephp and xdebug but they don't show when changes where made to the variable, only its final value.

Any solution?

nietonfir
  • 4,797
  • 6
  • 31
  • 43
shultz
  • 2,543
  • 3
  • 20
  • 23
  • 1
    I understand what you wanna do, but why, what is the reason? – Kylie Jun 08 '13 at 18:53
  • if you want to do is manually then at every change on variable you cant assign variable values to array like `$logs[]=$variable;` but you to do it at every change in the end just `dump($logs)` – M Khalid Junaid Jun 08 '13 at 18:56
  • In classes, you can do this using `__set` – kelunik Jun 08 '13 at 18:59
  • Debuging, I know for certainty that the end results is wrong. I've tried for days now to follow it step by step (and believe me there are many steps) but in vain. I hope this log will allow me to detect the cause of the problem. – shultz Jun 08 '13 at 19:01
  • Recode - could you please explain what you mean or give an example? – shultz Jun 08 '13 at 19:02
  • 1
    @Recode: You cannot do that unless the property you are setting is not declared on the class. And not declaring properties is a very bad way to code things. And of course it doesn't help if the variable holding the object is changed, only if a property of the object is. – Jon Jun 08 '13 at 19:22
  • @Jon you just have to set them to private (if you only access your class outside the class). – kelunik Jun 08 '13 at 19:26
  • @shultz have a look at php-docs, print the value of your variable in `__set`, then set it there and print the new value. – kelunik Jun 08 '13 at 19:27

2 Answers2

2

XDebug can track the variable changes, just enable xdebug.collect_assignments and xdebug.collect_params, so when you generate trace log file, you should see the changes.

Example configuration:

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.

Then before or after you assign the variable for the first time, start tracing the code by adding this into your PHP code:

xdebug_start_trace();

then optionally add xdebug_stop_trace(); at the end where you think it already happened.

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

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

or filter it into the smaller file by: grep pattern trace-log.xt > smaller.log.txt.


Another alternative would be to use phpdbg.

kenorb
  • 155,785
  • 88
  • 678
  • 743
1

May be logged decorator can help you?

If you want to track some instance variables you can wrap them around with decorator that implements same interface. And into decorator methods you can write debug level log and then deligate workflow to original variables saved as decorator object field.

Community
  • 1
  • 1
Ivan Velichko
  • 6,348
  • 6
  • 44
  • 90