Inspecting the contents of Magento models using debug() vs var_dump()

When you are developing for Magento, it is often helpful to see the contents of a Model. A model is a pretty complex object, so when you want to inspect the contents of the model, you generally don’t want to see all of its properties – all you’re really wanting to see is the contents of the _data property, which is the array that contains all of the attributes/fields that have been retrieved from the database.

Typically, when inspecting a model, I’ve used one of the following methods:

$model = Mage::getModel('catalog/product')->load(1);
# Method #1: This dumps all of the model's property's, including the _data property.
# This results in a lot of superfluous code that I don't need to see.
# Method #2: This only logs the data in the _data property.  The problem is, if the _data property contains
# any other models, all of the properties of those models will be dumped

I recently came across the ->dump() method that is implemented in the Varien_Object class. This method recurses into the _data property of a model and returns an array of the values in the _data property, including the _data properties of the child classes. Here’s the example code:

# Method #3

Note: The Varien_Object class is a class from which MANY classes in Magento extend. Most notably, all blocks and all data models extend Varien_Object. If you’ve never looked at the Varien_Object class, I’d recommend scanning/reading through the methods in that class. If you’re too lazy to read through the methods, but want to see what the debug() method does, I’ve included it at the bottom of this post.

Here are three screenshots exemplifying the difference between the different methods (note: the beautifully formatted var_dump output is a feature of the xdebug php extension):

Method 1:

Method 1 - var_dump

Method 1 – var_dump

Method 2:

Method 2 - getData

Method 2 – getData

Method 3:

Method 3 - debug

Method 3 – debug

In case you’re curious to see how the debug() method works, here’s the code from the Varien_Object class:

     * Present object data as string in debug mode
     * @param mixed $data
     * @param array $objects
     * @return string
    public function debug($data=null, &$objects=array())
        if (is_null($data)) {
            $hash = spl_object_hash($this);
            if (!empty($objects[$hash])) {
                return '*** RECURSION ***';
            $objects[$hash] = true;
            $data = $this->getData();
        $debug = array();
        foreach ($data as $key=>$value) {
            if (is_scalar($value)) {
                $debug[$key] = $value;
            } elseif (is_array($value)) {
                $debug[$key] = $this->debug($value, $objects);
            } elseif ($value instanceof Varien_Object) {
                $debug[$key.' ('.get_class($value).')'] = $value->debug(null, $objects);
        return $debug;

I hope this quick tip helps you program with models more effectively! If you have any questions, feel free to post comments below.

Share it


Related Posts

Everything You Need to Know About Updating to Google Analytics 4

Classy Llama is Proud to be a Sponsor of Signifyd’s #FLOWSummit2023

Classy Llama Earns 2 New Workplace Awards from

Contact Us