php dubugger

Status
Niet open voor verdere reacties.

pkmartijn

Gebruiker
Lid geworden
18 aug 2011
Berichten
163
ik heb een debug class:
PHP:
<?php

class debug {

    static public $on = false;

    /**
    * @var array List of errors types
    */
    static private $error = array(
        E_ERROR           => 'Fatal error',
        E_WARNING         => 'Warning',
        E_PARSE           => 'Parse error', // won't be used....
        E_NOTICE          => 'Notice',
        E_CORE_ERROR      => 'Fatal error',
        E_CORE_WARNING    => 'Warning',
        E_COMPILE_ERROR   => 'Compile error',
        E_COMPILE_WARNING => 'Compile warning',
        E_USER_ERROR      => 'Fatal error',
        E_USER_WARNING    => 'Warning',
        E_USER_NOTICE     => 'Notice',
        E_STRICT          => 'Notice strict'
    );

    /**
    * @var array overloading methods
    */
    static private $overload = array(
        '__call' => 2,
        '__callStatic' => 2,
        '__get' => 1,
        '__set' => 1,
        '__clone' => 1,
        'offsetGet' => 1,
        'offsetSet' => 1,
        'offsetUnset' => 1,
        'offsetExists' => 1,
    );

    static private $time;

    static private $chrono;

    static public function handler ( $type, $message, $file, $line, $scope ) {
        global $php_errormsg; // set global error message regardless track errors settings
        $php_errormsg = self::clean_message( $message );
        if ( ! self::$on ) { // de-activate
            return false;
        }
        $stack = self::stack( debug_backtrace( false ), $type ); // clean stack depending if error is user triggered or not
        self::overload( $stack, $file, $line  ); // clean stack + switch line & file if overloaded method triggered the error
        echo '<div class="debug ', strtolower(str_replace(' ', '_', self::$error[$type])),'"><div class="message"><strong>',
            self::$error[$type], '</strong>: ', $php_errormsg, ' in <strong>',
             $file, '</strong> on line <strong>', $line, '</strong></div>'; // print error
        if ( $type & (E_ALL^E_NOTICE^E_USER_NOTICE) ) {
            self::context( $stack, $scope ); // print stack trace & context
        }
        echo '</div>';
        if ( $type & E_USER_ERROR ) { // fatal error
            exit;
        }
    }

    static protected function stack ( $backtrace, $type ) {
        $user_triggered = E_USER_ERROR|E_USER_WARNING|E_USER_NOTICE;
        return array_slice( $backtrace,  $type & $user_triggered ?  2 : 1 );
    }

    static protected function overload ( &$stack, &$file, &$line ) {
        if ( isset( $stack[0]['class'], self::$overload[$stack[0]['function']] )
          && $offset = self::$overload[$stack[0]['function']] ) {
            for ( $i = 0; $i < $offset; $i++ ) {
                extract( array_shift( $stack ) ); // clean stack and overwrite file & line
            }
        }
    }

    static protected function clean_message ( $message ) {
        return preg_replace( '~^.*</a>\]: +(?:\([^\)]+\): +)?~', null, $message ); // clean useless infos
    }

    static protected function context ( $stack, $scope ) {
        if ( ! empty($stack) ) {
            echo '<div class="stacktrace"><strong>Stack trace</strong>:<ol>';
            foreach ( $stack as $index => $call ) {
                echo '<li>';
                if ( isset( $call['file'] ) ) {
                    echo $call['file'], '[', $call['line'], ']';
                } else {
                    echo '[internal function]';
                }
                echo ': ', isset($call['class']) ? $call['class'] : null, isset($call['type']) ? $call['type'] : null,
                     $call['function'], '(', self::args_export( isset($call['args']) ? $call['args'] : null ), ')</li>';
            }
            echo '</ol></div>';
            if ( $scope && ! isset( $scope['GLOBALS'] ) ) {
                echo '<div class="context"><strong>Context</strong>:<ul>';
                foreach ( $scope as $name => $value ) {
                    echo '<li>$', $name,' = ', self::var_export( $value ), ';</li>';
                }
                echo '</ul></div>';
            }
        }
    }

    static protected function var_export ( $var ) {
        if ( is_array( $var ) ) {
            return 'array(' . self::args_export( $var ) . ')';
        }
        $output = var_export( $var, true );
        if ( is_object( $var ) ) {
            return 'object(' . substr( $output, 0, strpos( $output, ':') ) . ')';
        }
        return $output;
    }

    static protected function args_export ( $args ) {
        if ( is_array($args) ) { // format arguments if any
            return implode(', ',
                array_map( 'debug::var_export', $args )
            );
        }
    }

    static public function chrono( $print = null, $scope = '' ) {
        if ( ! isset( self::$time[$scope] ) ) {
            echo PHP_EOL, '<div class="debug chrono"><strong>', $scope, ' init', '</strong></div>', PHP_EOL;
        } elseif ( is_string( $print ) ) {
            echo '<div class="debug chrono">',
                sprintf('%s -> %s: %ss',
                    $scope,
                    $print,
                    round( self::$chrono[$scope][$print] = microtime(true) - self::$time[$scope], 6 )
                ), '</div>', PHP_EOL;
        } elseif ( $print && isset( self::$chrono[$scope] ) ) {
            asort( self::$chrono[$scope] );
            $base = reset ( self::$chrono[$scope] ); // shortest duration
            foreach( self::$chrono[$scope] as $event => $duration ) {
                $table[] = sprintf('%5s - %-38.38s <i>%7ss</i>',
                    round( $duration / $base, 2 ),
                    $event,
                    round( $duration, 3 )
                );
            }
            echo '<div class="debug"><strong>', $scope, ' chrono</strong><pre>',
                sprintf('%\'-61s %-46s<em>duration</em>%1$s%1$\'-61s',
                    PHP_EOL,
                    'unit - action'
                ), implode( PHP_EOL, $table ), '</pre></div>';
        }
        return self::$time[$scope] = microtime(true);
    }

}

set_error_handler  ( 'debug::handler', E_ALL );

?>

en nu wil ik hem aanroepen ik heb:
PHP:
<?

include ("debug.php");

$debug = new debug();
echo $debug
?>
maar hij geeft deze error:
Catchable fatal error: Object of class debug could not be converted to string in /public_html/martijn/debuger.php on line 7
weet iemand wat ik fout doe?
 
Wat verwacht je dan dat er gebeurt?

je maakt een object debug aan. prima. Maar je vult de class niet, en je roept ook geen functie aan van die class die een feedback geeft. Zoals: $debug.handler()
 
Hoe moet ik dan inbouwen dat je script erin doet en dat hij het controleerd?
 
Je heb een class met static methodes. Die kun je niet innitsieren.

je kunt bv de chrono method aansturen met

debug::chrone()
 
hoe moet ik dan bijv dit script controleree

PHP:
<?
$test = hoi
echo $test

?>
ik wil dan dat hij zegt er moet nog een ; maar hoe doe ik dat dan in mijn script zettn?
 
geen idee, iets als
debug::handler("php", "bericht", "test.php", true);
Maak zonder documentatie is dat lastig te zeggen.
Maar waarom zo omslachtig? En download je niet Netbeans.
 
Tja php is niet zo makkelijk als het lijkt :)
 
neem eens een kijkje op you tube, en zoek bv op netbeans php install tut
zeker weten dat er wel een filmpje tussen staat hoe het te installeren en te gebruiken.
Als je het eenmaal door hebt script het heerlijk
 
wat bedoelen ze bij ftp toevoegen met project Url?
als ik martijn.tycal.org/mailer/index.php doe zegt hij dat dat niet geldig is
 
Status
Niet open voor verdere reacties.
Steun Ons

Nieuwste berichten

Terug
Bovenaan Onderaan