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.

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.

PHP Code:
/**
     * 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:

PHP Code:
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

PHP Code:
Mage::log($foonull'mylogfile.log'); 
You can also use combinations of functions/methods to output the contents of an array for instance:

PHP Code:
Mage::log(var_dump($fooArray), 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");

PHP Code:
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.

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.

PHP Code:
print "<pre>"var_export(get_class_methods(get_class($this))); 
You can also use it in combination with Magento’s getData() to display data with key values intact.

PHP Code:
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:

PHP Code:
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:

Code:
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 this post

View more threads in the same category: