Magento Debugging Basics

Magento is a big system. If you are developing for Magento it is inevitable that you are going to either need to trace down a big or just figure out how a particular process works.

The best thing to do is to start with the controller that is triggered when the process starts. If you are not able to figure out which controller you are starting from, I suggest you buy Commerce Bug by Alan Storm. It will take all the guess work out of that for you as well as tell you a bunch of other information about the page you are currently viewing.

Now that you know where to start, lets talk about the functions and commands I find most useful when debugging Magento.

echo “captured”; exit(); – This does exactly what it looks like. Echos out the word “captured” and stops execution. This is helpful so that you can verify that the code you think is running, is in fact running. It is also a good to use in conjunction with the other commands so you can stop the process without loading the layout or template of the site.

echo get_class($obj); – This will simply tell you the name of the class that $obj is. Since class names in Magneto (and the Zend Framework for that matter) tell you where the actual file is located, this is very helpful so you can follow the next step of the code.

print_r(get_class_methods($obj)); – get_class_methods returns a list of explicitly defines methods that $obj currently has. I say explicitly defines because it does not display functions that come from Magento’s magic functions (all the nice getters and setters).

debug_stacktrace() – This function is good when you know the end point of the code you want to debug but want to see where it was called from. This displays a complete stack trace of the code from the point this function is called.

print_r($obj->debug()); – Nearly every single object in Magento extends Varien_Object. One of the methods that Varien_Object has is debug. This function will show you the data that this object currently holds.

Those are the very basics, but you can trace Magento code very easily with these few functions. What other functions do you use to debug Magento?