📜 ⬆️ ⬇️

Easy debugging with PHPixie Debug

image
PHPixie Debug was created to improve debugging when developing in any environment. Of course, if you are already using a web framework, then most likely there are no problems with debugging, but when you are writing a library, solving a puzzle, or even working with Wordpress, then there is a great lack of convenient debugging. Even for such a basic functionality, how to turn all errors into exceptions, you have to register your handler. In such cases, PHPixie Debug in just two lines of code will create a convenient development environment.

Exceptions and Traces


Debug provides in the console the convenience of working with exceptions to which we are already accustomed to in web frameworks. When writing libraries, I really lacked beautiful informative traces with the output of the part of the code in which the exception occurred. The big problem with tracing in PHP is that if you simply call print_r (debug_backtrace ()) and one of the arguments of any call is an object, the result will turn into a mountain of text. There is, of course, debug_print_backtrace () , but firstly, it still outputs the arrays completely, and secondly, to get it as a string, you have to use a buffer. And this is how the PHPixie Debug exceptions process will handle:

<?php require_once('vendor/autoload.php'); $debug = new \PHPixie\Debug(); try{ throw new \Exception("test"); }catch(\Exception $e) { //  $debug->exceptionMessage($e); } echo "\n-------\n"; //   //       //(    ) $debug->registerHandlers(); class Test { public function a($string) { $array = array(1, 2); $this->b($string, $array); } public function b($string, $array) { $object = (object) array('t' => 1); $this->c($string, $array, $object); } public function c() { substr(); } } $test = new Test(); $test->a("pixie"); 

')
 Exception: test 5 6 try{ > throw new \Exception("test"); 8 9 }catch(\Exception $e) { #0 D:\debug\examples\exceptions.php:7 ------- ErrorException: substr() expects at least 2 parameters, 0 given 36 public function c() 37 { >> substr(); 39 } 40 } #0 D:\debug\examples\exceptions.php:38 #1 D:\debug\examples\exceptions.php:38 substr() #2 D:\debug\examples\exceptions.php:33 Test->c('pixie', array[2], stdClass) #3 D:\debug\examples\exceptions.php:27 Test->b('pixie', array[2]) #4 D:\debug\examples\exceptions.php:43 Test->a('pixie') Logged items: 

By the way, please note that in the trace the handler who turned the PHP error into an exception is not visible, many other libraries often forget to hide it, thus clogging up the trace. PHPixie hides all such interventions in the call stack.

Variable Output


The output is made through the static function \ PHPixie \ Debug :: dump () , this is by the way the first static function in PHPixie. The reason for this is that, according to the concept, the debugging functions are not part of your application and you already delete them as soon as everything works. Therefore, it makes no sense to pass the Debug class as a dependency through the constructor. By the way, it is worth noting that these functions will work only if the Debug object itself has already been created, so they are just a proxy and there will never be any static logic in PHPixie.

 <?php require_once('vendor/autoload.php'); use PHPixie\Debug; $debug = new Debug(); Debug::dump("Array dump:"); Debug::dump(array(1)); Debug::dump("Short array dump:"); //    . //      //      Debug::dump(array(1), true); $object = (object) array('t' => 1); Debug::dump("Object dump:"); Debug::dump($object); Debug::dump("Short object dump:"); Debug::dump($object, true); Debug::dump("Dump trace with parameters"); class Test { public function a($string) { $array = array(1, 2); $this->b($string, $array); } public function b($string, $array) { $object = (object) array('t' => 1); $this->c($string, $array, $object); } public function c() { Debug::trace(); } } $t = new Test(); $t->a("test"); 


 'Array dump:' Array ( [0] => 1 ) 'Short array dump:' array[1] 'Object dump:' stdClass Object ( [t] => 1 ) 'Short object dump:' stdClass 'Dump trace with parameters' #0 D:\debug\examples\dumping.php:37 PHPixie\Debug::trace() #1 D:\debug\examples\dumping.php:32 Test->c('test', array[2], stdClass) #2 D:\debug\examples\dumping.php:26 Test->b('test', array[2]) #3 D:\debug\examples\dumping.php:42 Test->a('test') 


Log


In order to separate the debugging messages from the output of the program itself, often the messages are written into an array which is output at the end of the program execution. The only problem is that if an error occurs or exit () is used somewhere, then after this message is no longer displayed. PHPixie Debug displays a log with exceptions and can also register a handler for the mandatory output of the message log at the end of the script. Here are two examples:

 use PHPixie\Debug; $debug = new Debug(); Debug::log("test"); Debug::log(array(3)); class Test { public function a($string, $num) { Debug::logTrace(); } } $t = new Test(); $t->a("test", 5); //   $debug->dumpLog(); 


 Logged items: [0] D:\debug\examples\logging.php:7 'test' [1] D:\debug\examples\logging.php:8 Array ( [0] => 3 ) [2] D:\debug\examples\logging.php:16 #0 D:\debug\examples\logging.php:16 PHPixie\Debug::logTrace() #1 D:\debug\examples\logging.php:20 Test->a('test', 5) 


And with automatic output:

 <?php use PHPixie\Debug; $debug = new Debug(); // 'true'    //       // $debug->registerHandlers(true); Debug::log("test"); echo("Logged messages will be printed below"); 


 Logged messages will be printed now Logged items: #0 D:\debug\examples\log_handler.php:13 'test' 


Finally


The main goal of the library is not the output of the traces and the log itself, but rather a convenient presentation of them in the form of classes, which will allow in the future to build a web version of debug for PHPixie, because in fact it remains only to add a beautiful rendering template. But the main advantage of Debug as a standalone tool is that with just two lines of additional code, you can get a convenient environment for developing non-web applications. I hope the next time when an employer offers you to solve a traveling salesman problem or you need tracing in Wordpress, you will remember this library and save time you would spend on debug_backtrace () .

Demo


To try Debug do it yourself is enough:

 git clone https://github.com/phpixie/debug cd debug/examples #      curl -sS https://getcomposer.org/installer | php php composer.phar install php exceptions.php php logging.php php log_handler.php php dumping.php 


And by the way, like all other libraries from PHPixie, you will be 100% covered in code by tests and work under any version of PHP older than 5.3 (including the new 7 and HHVM).

Source: https://habr.com/ru/post/257625/


All Articles