2013-06-08 1 views
4

좋은 하루.PHP에서 변수 변경을 추적하는 방법

저는 많은 변수와 세션을 포함하고있는 프로젝트에서 일하고 있으며 대부분의 작업은 "내부적으로"그리고 ajax를 통해 이루어졌습니다.

문제는 프로젝트를 디버그하려고하는데 특정 변수에 대한 변경 사항을 추적하고 기록하는 방법을 찾을 수 없다는 것입니다.

나는 firephp와 xdebug를 사용하려고 시도했지만 변수가 만들어지면 변경 사항을 표시하지 않으며 최종 값만 표시합니다.

모든 솔루션?

+1

나는 당신이하고 싶은 것을 이해하지만 왜, 그 이유가 무엇인가? – KyleK

+0

당신이하고 싶다면 수동으로 할 때마다 변수를 변경할 때마다 변수 값을'$ logs [] = $ variable; '과 같이 배열에 할당하지만 끝에있는 모든 변경에서 변수 값을 지정하면된다.'dump ($ logs) ' –

+0

클래스에서, 당신은'__set'을 사용하여 이것을 할 수 있습니다. – kelunik

답변

1

기록 될 수 있습니다. decorator이 도움이 될 수 있습니까?

일부 인스턴스 변수를 추적하려는 경우 동일한 인터페이스를 구현하는 데코레이터로 래핑 할 수 있습니다. 데코레이터 메소드에 디버그 레벨 로그를 작성한 다음 데코레이터 객체 필드로 저장된 원래 변수로 워크 플로우를 구분할 수 있습니다.

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을 사용하는 것입니다.