Jump to content

Debugging Class Files Idea


Slagar

Recommended Posts

I'm writing class files, and often use the same typical format for each class (think standard database/web form classes). As I'm constantly trying to refine the way I do things, I'm wondering if this would be good practice.

Basically, I'm thinking of using custom error reporting in my class files. Have a simple constant at the top of the class file, and then within the class at critical places, have some custom errors.

Here's a silly example of some Person class:

 

define("DEBUG_MODE", "on");

class Person {
...
function SetName($name) {
   if (strlen($name) > 0) {
      $this->name = $name;
      return true;
   } else {
      if (DEBUG_MODE == "on") {
         echo("<strong>Custom Error:</strong> ".$name." is not a valid name.");
      }
      return false;
}

 

Ok, it's a trivial example (sorry for any errors in the above; I just made it up). My point is, would something like this be worthwhile, or would it needlessly bloat my class files? I don't usually find debugging to be a problem, and realistically most of the stuff I write isn't super complex... what do you think? :)

Link to comment
Share on other sites

I think it would needlessly bloat your class files. Your best bet would be to learn a good code debugger that allows you to step through your code line by line... alternatively you can insert those echo statements when trying to debug... but I wouldn't leave them there.

Link to comment
Share on other sites

What I generally do for debugging is actually create a seperate class for debugging.  Make sure it's attached to all pages (just like your normal config classes would be). Then you can do a simple file log for any errors and/or messages you need.  This will generate a file that will can view that shows the errors.  If you want a good example Cakephp framework has a very good functioning example of this.

 

Anywhere through there system you can perform...

$this->log();

Within that function you can pass a string of what to print out and/or just pass it as is and it'll show an error was thrown.  You can also use it to pass an array into it and it'll print out the array into the log file.  I recommend you download cakephp and break open the code and hunt down there log function.  It'll teach you everything you need to know.  It saves all of the log information into the log folder.  If you pass it a constant for DEBUG then it prints out the array and creates a debug.txt for it.  If not then it just shows in error.log that it was an error and what time and everything else.

 

Looking at it function is a marvel.  Dig through the code for a minute and you could easily replicate a stand alone class that can do the same thing (it's just simply saving something into a file).  This is VERY VERY helpful when doing heavy ajax.  It's sometimes hard to see what is being returned when doing an ajax only call...being able to log it and check the file afterwards is VERY helpful.

 

If you need anymore help post here.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.