Archive for May 2013

Magento : Initial Setup Development Tips

1. Automatically turn on developer mode based on domain.

We have a stage site setup for all of our projects at We also develop projects locally at either .dev or .local (based on developer preference). This snippet of code will only enable error reporting and Magento’s developer mode on stage or local domains.

Insert the following code after “require_once $mageFilename;” in index.php


2. Enable logging

Magento has a built-in logging method that allows you to log any variable type to a log file. This is very helpful when building Magento modules, as you can easily inspect data structures, without having to open a debugging session to inspect the variables in local scope. By default, logging is turned off in Magento. To enable logging, go to the “Developer” tab on the “System > Configuration” page. Change the “Enabled” select list under the “Log Settings” section to “Yes” and then save the page.

You can log variables to the Magento log using the following code: Mage::log($variable); By default, logs are stored in var/log/system.log

Here are some example usages of Mage::log()



If you’re developing on a Mac, I’d recommend opening the system.log file with the Console app. If you’re on a *nix based machine, you “tail” the latest contents of the log file using the following bash command:
tail -f



3. Enhance error backtraces

All of our developers use xdebug as a part of their Apache configuration. Xdebug has two main benefits: (1) It allows us to use PDT to debug PHP applications. (2) It overrides the default PHP error messages with detailed, fully customizable error backtraces. You can see an example backtrace below:

This is what that error would look like without xdebug:


Magento has built-in error and exception handling. Since errors are being handled by Magento, we have to modify a core file to make Magento let PHP/xdebug handle the displaying of the messages.

In a future blog post, will cover how to make Magento let xdebug handle exceptions

In app/code/core/Mage/Core/Model/App.php, replace the setErrorHandler() method with the setErrorHandler() method below:

Modifying core Magento files is never recommended, but if Magento gets upgraded and this change gets overridden, there won’t be an issue, since it’s not critical for the site to function.

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)


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.


And here is an example:

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.


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.

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.


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:


Then use .htaccess SetEnv to enable and disable developer mode per environment: