Вот класс, который позволяет отслеживать изменения по глобальным переменным:
abstract class CSTReportDelegate {
abstract public function emitVariableChange( $variableName, $oldValue, $newValue );
abstract public function emitVariableSetNew( $variableName, $newValue );
}
class CSTSimpleReportDelegate extends CSTReportDelegate {
public function emitVariableChange( $variableName, $oldValue, $newValue ) {
echo '<br />[global/change] '. $variableName . ' : ' . print_r( $oldValue, true ) . ' → ' . print_r( $newValue, true );
}
public function emitVariableSetNew( $variableName, $newValue ) {
echo '<br />[global/init] '. $variableName . ' → ' . print_r( $newValue, TRUE );
}
}
class CSysTracer {
static protected
$reportDelegate;
static private
$globalState = array();
static private
$traceableGlobals = array();
static private
$globalTraceEnabled = FALSE;
static public
function setReportDelegate( CSTReportDelegate $aDelegate ) {
self::$reportDelegate = $aDelegate;
}
static public
function start( ) {
register_tick_function ( array( 'CSysTracer', 'handleTick' ) );
}
static public
function stop() {
unregister_tick_function( array( 'CSysTracer', 'handleTick' ) );
}
static public
function evalAndTrace( $someStatement ) {
declare( ticks = 1 ); {
self::start();
eval( $someStatement );
self::stop();
}
}
static public
function addTraceableGlobal( $varName ) {
if ( is_array( $varName )) {
foreach( $varName as $singleName ) {
self::addTraceableGlobal( $singleName );
}
return;
}
self::$traceableGlobals[ $varName ] = $varName;
}
static public
function removeTraceableGlobal( $varName ) {
unset( self::$traceableGlobals[ $varName ] );
}
/**
* Main function called at each tick. Calls those functions, which
* really perform the checks.
*
*/
static public
function handleTick( ) {
if ( TRUE === self::$globalTraceEnabled ) {
self::traceGlobalVariable();
}
}
static public
function enableGlobalsTrace() {
self::$globalTraceEnabled = TRUE;
}
static public
function disableGlobalsTrace() {
self::$globalTraceEnabled = FALSE;
}
static public
function traceGlobalVariable( ) {
foreach( self::$traceableGlobals as $aVarname ) {
if ( ! isset( $GLOBALS[ $aVarname ] )) {
continue;
}
if ( ! isset( self::$globalState[ $aVarname ] ) ) {
self::$reportDelegate->emitVariableSetNew( $aVarname, $GLOBALS[ $aVarname ] );
self::$globalState[ $aVarname ] = $GLOBALS[ $aVarname ];
continue;
}
if ( self::$globalState[ $aVarname ] !== $GLOBALS[ $aVarname ]) {
self::$reportDelegate->emitVariableChange( $aVarname, self::$globalState[ $aVarname ], $GLOBALS[ $aVarname ] );
}
self::$globalState[ $aVarname ] = $GLOBALS[ $aVarname ];
}
}
}
Использовать его так:
CSysTracer::addTraceableGlobal( array( 'foo', 'bar' ));
CSysTracer::setReportDelegate( new CSTSimpleReportDelegate() );
CSysTracer::enableGlobalsTrace();
CSysTracer::start();
declare( ticks = 1 );
$foo = 10;
echo "<br>First output";
$foo *= $foo;
$foo = 'bar';
echo "<br>Next output";
$otherFoo = array (1,2,3);
$bar = 23;
echo "<br>Finished!";
CSysTracer::stop();
Whill output
[global/init] foo → 10
Hi!
[global/change] foo : 10 → 100
[global/change] foo : 100 → bar
Gotta run
[global/change] foo : bar → Array ( [0] => 1 [1] => 2 [2] => 3 )
[global/init] bar → 23
Bye!
В то время как эта версия CSysTracer отслеживает изменения в глобальных переменных, вы можете создать вариант traceGlobalVariable () для отслеживания изменений в переменных сеанса или, например, вызовах методов.