EN | CS | Login | Register

(this page is translated by Google; We're working hard on a human translation)

Nette\Debug

Library Nette\Debug, which found a home under the name Laděnka, everyday is a useful helper PHP programmer.

Capture errors and exceptions

Catching errors is the best turn at the very beginning, immediately after retrieval net.

 require LIBS_DIR . '/Nette/loader.php' ; 
Debug::enable();

Call Debug::enable() activates laděnku which alone detects whether the running of the development or production Map. Exception of the development shows the user-developers, the production error logs, or send information by e-mail. To establish whether the net for the running production, use Map method Environment::isProduction() . For more information, see the Debug API class .

Report on the unrecognized exception or error provides important information to developers about where and why it happened. Standard output in PHP looks like this:

The standard form of unrecognized exceptions

But let's get the word Laděnku. After activating the command Debug::enable() will show us their sexiest form:

Uncaught exception in the execution Nette\Debug

Here is the exception , so it looks generated an error . It is then a different tune, huh? Thus they are automatically displays all the fatal errors. For even deeper odvšivování can turn strict mode. He will appear in the same way as errors of lower levels as E_NOTICE and E_WARNING .

 Debug:: $strictMode = TRUE ; 

Individual parts of the screen can also conveniently mice rozklikávat:

But there are situations where a certain piece of code we want to be treated through the try/catch and not thrown any exceptions must be followed by the end of the script. Method processException () is responsible for display / log the exception and pass control back to the application, unlike exceptionHandler () , which after processing the exception of activities the program finishes.

Variable dump

Each tuner is a good friend with the function var_dump , which lists in detail the contents of variables. Unfortunately, in an HTML listing lose formatting and decanted into a single line of HTML code sanitizaci say nothing. In practice, it is necessary to var_dump replace the handier features. Tou is now Debug::dump()

 $arr = array ( 10 , 20 . 2 , TRUE , NULL , 'hello' ); 

Debug::dump( $arr );
// včetně jmenného prostoru Nette\Debug::dump($arr);

generates the output:

 <pre><span  style= "color:gray" > array </span> (5) { 
[ 0 ] => <span style= "color:gray" >int</span>( 10 )
[ 1 ] => <span style= "color:gray" >float</span>( 20 . 2 )
[ 2 ] => <span style= "color:gray" >bool</span>( true )
[ 3 ] => <span style= "color:gray" > NULL </span>
[ 4 ] => <span style= "color:gray" >string</span>( 5 ) "hello"
}
</pre>

Timing

Another useful instrument tuner has a stopwatch with an accuracy of microseconds:

 Debug::timer(); 

// princi můj malinký spi, ptáčkové sladce již sní...
sleep ( 2 );

$elapsed = Debug::timer();
// $elapsed ≈ 2

Optional parameters can be achieved by multiple measurements.

 Debug::timer( 'page-generating' ); 
// nějaký kód
Debug::timer( 'rss-generating' );

// nějaký kód
$rssElapsed = Debug::timer( 'rss-generating' );
$pageElapsed = Debug::timer( 'page-generating' );

Profiler

Profiler is enabled using the Debug::enableProfiler();

Has its own API, and supports drag & drop. Add more information can be very easily:

 Debug:: $counters [ 'Last SQL query' ] = & dibi:: $sql ; 
Debug:: $counters [ 'Nette version' ] = Framework::VERSION . ' ' . Framework::REVISION;

Debug::addColophon( array ( 'dibi' , 'getColophon' ));

All output can be disabled by calling the profiler

 Debug::disableProfiler(); 

See also:


Login to submit a comment