Debugging tips & tricks with Magento Commerce

If you are new to Magento then the vast amount of code to digest can be quite overwhelming. However, here are some tips I’ve learned about over the past on some means to tear down Magento and figure out whats making it work.
Zend_Debug::dump
First off use Zend_Debug::dump($foo); instead of using var_dump($foo); or print_r($foo); it is essentially the same, just a wrapper with pre HTML tag for formatting and escaping of special characters.
More details about
Zend Frameworks dump method.
However there will be times that simply dumping objects to the screen can be too much and cause browser hangups or even crashes. The best practice Ive learned is to always use the getData() method that Magento has built-in to the Varient Object, this way your not getting redundant amounts of data dumped to the screen but only the bits you really care about.
Varien Object getData, debug
Magento also has a built-in debug() method in the Varient Object as well that you can use to display data in a string representation of itself.
Keep in mind debug does NOT always get attached to every object.
More details about the
Varien Object debug method,
Varien Object getData method
Log files
What if your having difficulty displaying things to screen or don’t want any users to see debug output. With this in mind you can also use a built in logging function, similar to Zend_Log and is essentially a wrapper as well. You can find it defined in app/Mage.php.
/**
* log facility (??)
*
* @param string $message
* @param integer $level
* @param string $file
* @param bool $forceLog
*/
public static function log($message, $level = null, $file = '', $forceLog = false)
...
And here is an example:
Mage::Log($foo);
Which will log the output the contents of $foo to /var/log/system.log by default.
You can also specify your own log file with an extra argument, your custom log file will appear in /var/log/mylogfile.log
Mage::log($foo, null, 'mylogfile.log');
You can also use combinations of functions/methods to output the contents of an array for instance:
Mage::log(var_dump($fooArray), null, 'mylogfile.log');
Logging MUST be enabled within the admin: Configuration -> Developer -> Log Settings -> Enabled = Yes
XML Configuration
Most of the time, I have have issues with my XML configurations. Since Magento is very configuration based driven, one improper case or underscore can render things useless. Magento doesn’t validate the XML or throw any errors when such are encountered but rather ignored. The best means I’ve found to figure out whats going on is to display the entire XML built from all XML configurations files with.
header("Content-Type: text/xml");
die(Mage::app()->getConfig()->getNode()->asXML());
xDebug
xDebug is probably one of the more well known and most used debugging tools available. If your IDE does support it, I would highly suggest taking the time to get your environments setup so that you can connect and use it. I’m not going to cover the general use and configuration of it, however Classy Llama has a nifty post that helps keep Magento from letting xDebug take over error handling.
Classy Llama’s Enable xDebugs Error Handler
It requires modification to the Core files and cant be extended since the class is final. Make note of your change when doing this, or merely use it on a per need basis and removing it after your done with it. You can also setup your version control to ignore any changes with it.
Built-in PHP functions
If your using a bare bones editor without any type of auto complete looking up available class methods can be a pain digging through Magento’s numerous files and folders. To get all available methods from any class you can use var_export, get_class_methods and get_class in combination.
print "<pre>"; var_export(get_class_methods(get_class($this)));
More details on: var_export(), get_class(), get_class_methods()
You can also use it in combination with Magento’s getData() to display data with key values intact.
print "<pre>"; var_export(array_keys($this->getData()));
Developer Mode
One last tip I’ve been doing lately is modifying index.php and adding ini_set('display_errors', 1); to the condition checking for the developer mode flag: MAGE_IS_DEVELOPER_MODE. By default the display_errors ini_set is commented out. Here is what my change looks like:
if (isset($_SERVER['MAGE_IS_DEVELOPER_MODE'])) {
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
}
Then use .htaccess SetEnv to enable and disable developer mode per environment:
SetEnv MAGE_IS_DEVELOPER_MODE "true"
If you have any tips or if I missed something please feel free to comment and I’ll add it to the article.
Possibly Related Posts:
- Getting Started with Magento
- Magento vs X-Cart
- Magento version 2.0 (X.Commerce) and eBay
- Magento Supercharged Development Tools and Links
- Book Review: Magento 1.4 Themes Design by PacktLib
11Apr2011
4 Responses to Debugging tips & tricks with Magento Commerce Add your comment
Leave a Reply
You must be logged in to post a comment.









Magento eCommerceu00a0is a wonderful and powerful e-commerce.u00a0…Developing in Magento without theseu00a0debuggingu00a0tips can be challenging, especiallyu00a0..
Developing in Magento without these debugging tips can be. Magento eCommerce is a wonderful and powerful e-commerce.
We’ve come full circle in a way. I’m looking forward to the return of vibrant discussions as well.u00a0
Great Article!
In addition:
You can use:
- Varien_Debug::backtrace() for backtracing
- Varien_Profiler::start() / stop() for profiling
…
Moreover you need to configure your Magento in development mode as:
http://www.blog.magepsycho.com/configuring-magento-for-development-debug-mode/
Cheers!