Magento : Debugging tips & Tricks

If you are new to Magento then the vast amount of code to digest can be quite overwhelming. You can debug the code via this post . First of all enable the Log in magento . ( See How to enable the Log in magento)

Zend_Debug::dump

First off use Zend_Debug::dump($Array_Items); instead of using var_dump($Array_Items); or print_r($Array_Items); 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  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($log_Item);
Which will log the output the contents of $log_Item 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($log_Item, null, ‘mylogfile.log’);
You can also use combinations of functions/methods to output the contents of an array for instance:

Mage::log(var_dump($Array_Items), null, ‘mylogfile.log’);

 

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.

More details about Classy Llama’s Enable xDebugs Error Handler

Remark : 

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”

Leave a Reply

Your email address will not be published.