patternphpMinor
PHP Debugger Object needs review
Viewed 0 times
needsphpobjectdebuggerreview
Problem
I'm a pretty visual person and I like to see all the cool information about objects, arrays and different variables in PHP. In the beginning I would just
This was my attempt at an OOP-style debugger that will spit out various information about the passed variable. The
My question is: Does this look horribly bad? Are there ways that my code can be improved? Is my logic illogical?
```
/**
* Returns useful information about variables or objects for debugging purposes
*
* @package sprayfire
* @subpackage core
*/
class SprayFireDebugger {
/**
* The original variable that needs to be debugged
*
* @var mixed
*/
private $_debugVar;
/**
* True if the debug message should be sent to console.log and false if it
* doesn't
*
* @var bool
*/
private $_doConsoleLog;
/**
* The final message that should be echod
*
* @var mixed
*/
private $_debugMessage;
/**
* SprayFireDebugger should never rely on __construct dependencies
*
* @return void
*/
public function __construct() {
}
/**
* Will emit useful information about the given variable. If the second
* parameter is set to false the message gets displayed on the screen
* as compared to the debug console
*
* @param mixed $var
echo '', var_dump($var), ''; liberally throughout my code. Then I was introduced to MVC and started grasping the concept of OOP and the Front Controller design pattern. Now, after having built my own little personal framework for academic reasons and understanding the need for reusable code a little part of my soul is eaten away every time I have to manually type this out.This was my attempt at an OOP-style debugger that will spit out various information about the passed variable. The
debug() method is the only public method and it takes two parameters, 1 being the variable you want to know more about and the other an optional boolean, default true, to toggle echoing a message to Firebug/Webkit Inspector console or straight to the browser screen.My question is: Does this look horribly bad? Are there ways that my code can be improved? Is my logic illogical?
```
/**
* Returns useful information about variables or objects for debugging purposes
*
* @package sprayfire
* @subpackage core
*/
class SprayFireDebugger {
/**
* The original variable that needs to be debugged
*
* @var mixed
*/
private $_debugVar;
/**
* True if the debug message should be sent to console.log and false if it
* doesn't
*
* @var bool
*/
private $_doConsoleLog;
/**
* The final message that should be echod
*
* @var mixed
*/
private $_debugMessage;
/**
* SprayFireDebugger should never rely on __construct dependencies
*
* @return void
*/
public function __construct() {
}
/**
* Will emit useful information about the given variable. If the second
* parameter is set to false the message gets displayed on the screen
* as compared to the debug console
*
* @param mixed $var
Solution
Does this look horribly bad?
It actually looks great:
I would go as far as saying it's the second most well written piece of php code I've seen on my fairly limited time on Code Review. And it's actually useful. I'm using a similar class on a 2*10^6 sloc project and it's extremely helpful. You should explore integrating xdebug into your class, for some bonus points.
Are there ways that my code can be improved?
There always are. But your code is at a point that you should probably spent time on other stuff, than trying to go the extra mile.
A very minor suggestion: I absolutely hate prefixing private / protected methods and members with an underscore. This:
doesn't work for me. It's a well known convention that's used all around, but what happens if at a later point you decide to make one of those public? Proponents of the underscore prefix always say it's a readability thing. Well it might be, but it doesn't matter:
Is my logic illogical?
Nope.
It actually looks great:
- Perfectly consistent
- Code is self documented, simple and broken into appropriate blocks (your functions make sense)
- phpDoc everywhere!
I would go as far as saying it's the second most well written piece of php code I've seen on my fairly limited time on Code Review. And it's actually useful. I'm using a similar class on a 2*10^6 sloc project and it's extremely helpful. You should explore integrating xdebug into your class, for some bonus points.
Are there ways that my code can be improved?
There always are. But your code is at a point that you should probably spent time on other stuff, than trying to go the extra mile.
A very minor suggestion: I absolutely hate prefixing private / protected methods and members with an underscore. This:
private $_debugVar;doesn't work for me. It's a well known convention that's used all around, but what happens if at a later point you decide to make one of those public? Proponents of the underscore prefix always say it's a readability thing. Well it might be, but it doesn't matter:
- All half decent IDEs have code explorers / outlines with different coloring schemes for different visibility scopes. Most of those views are sortable on visibility too.
- All half decent IDEs will only expose public methods / members via code completion when using the class.
Is my logic illogical?
Nope.
Code Snippets
private $_debugVar;Context
StackExchange Code Review Q#3301, answer score: 5
Revisions (0)
No revisions yet.