Sie kann…

  • eine Debug Nachricht erzeugen(msg)
  • die vergangene Zeit zwischen zweien Punkten (startTimer, stopTimer) messen
  • einen Variablen ausgeben(auch Klassen und Arrays) (varDump)
  • den aktuellen Call-Stack ausgeben(trace)

Die eigentliche Ausgabe erfolgt dabei, dem MVC-Muster zufolge, erst später. Dazu müssen die Debugdaten von der Klasse abgeholt(getDebugStack) und ausgegeben werden.

<?php
class fx_Debug
  extends fx_Singleton
{
  private $_debugging = false;
  private $_debugStack = array();
  private $_timerStack = array();
 
  static public function setDebugging($v)
  {
    self::getInstance()->_debugging = $v;
  }
 
  static public function isDebugging()
  {
    return self::getInstance()->_debugging;
  }
 
  static public function msg($msg = null)
  {
    $thiz = self::getInstance();
    if( $thiz->_debugging === false )
      return;
    $thiz->_debugStack[] = array('type'=>'msg',
                'msg'=>$msg,
                'trace'=>$thiz->_getTrace(true));
  }
 
  static public function startTimer($msg = '')
  {
    $thiz = self::getInstance();
    if( $thiz->_debugging === false )
      return;
    $thiz->_timerStack[] = array('msg'=>$msg, 'time'=>microtime(true));
  }
 
  static public function stopTimer()
  {
    $thiz = self::getInstance();
    if( $thiz->_debugging === false )
      return;
    $timer = array_pop($thiz->_timerStack);
    if( $timer === null )
      return;
   
    $thiz->_debugStack[] = array('type'=>'timer',
                'msg'=>$timer['msg'],
                'time'=>(microtime(true)-$timer['time']),
                'trace'=>$thiz->_getTrace(true));
  }
 
  static public function varDump($variable, $msg = null)
  {
    $thiz = self::getInstance();
    if( $thiz->_debugging === false )
      return;
   
    ob_start();
    var_dump($variable);
    $varDump = ob_get_contents();
    ob_end_clean();

    $varDump = htmlspecialchars($varDump);

    $thiz->_debugStack[] = array('type'=>'vardump',
                'msg'=>$msg,
                'vardump'=>$varDump,
                'trace'=>$thiz->_getTrace(true));  
  }
 
  static public function trace($msg = null)
  {
    $thiz = self::getInstance();
    if( $thiz->_debugging === false )
      return;
    $thiz->_debugStack[] = array('type'=>'trace',
                'msg'=>$msg,
                'traceFull'=>$thiz->_getTrace(false),
                'trace'=>$thiz->_getTrace(true));
  }
 
  static public function getDebugStack()
  {
    return self::getInstance()->_debugStack;
  }
 
  private function _getTrace($getOne)
  {
    $callStack = debug_backtrace(false);

    while( $callStack[0]['file'] === __FILE__ )
      array_shift($callStack);
   
    if( $getOne === true )
      return $callStack[0];
     
    return $callStack;
  }
}
?>