Figuring out Magento object context

Figuring out Magento object context

One of the problems working under the hood of the Magento CMS is determining the context of $this. If you are about to do any advanced stuff with your template, besides layout changes, you need to get familiar with Magento’s objects (classes).

Let’s have a look at the /app/design/frontend/default/default/template/catalog/product/view.phtml file. If you open this file and execute var_dump($this) your browser will return empty page after a short period of delay. By page I mean on the product view page; the one you see when you click on Magetno product. Experienced users will open PHP error log and notice the error message caused by var_dump(). Error message:
PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 121374721 bytes)

I like using print_r() and var_dump(), mostly because so far I had no positive experience using fancy debug or any other debugger with systems like Magento.

Why is PHP throwing errors then? If you google out the error PHP has thrown at you, you’ll see that PHP 5.2 has memory limit. Follow the link to see how and what exactly.

Google search gave me some useful results trying to solve my problems. I found Krumo at It’s a PHP debugging tool, replacement for print_r() and var_dump(). After setting up Krumo and running it on Magento it gave me exactly what I wanted. It gave me the object type of the dumped file; in this case it gave me object type of $this.

If you’re using an IDE studio with code completion support like NuSphere PhpED, ZendStudio or NetBeans and you decide to do something like $this-> you won’t get any methods listed. I haven’t yet seen the IDE that can perform this kind of smart logic and figure out the context of $this by it self.

What you can do is use the information obtained by krumo::dump($this). Performing krumo::dump($this) on /app/design/frontend/default/default/template/catalog/product/view.phtml file will return object type, Mage_Catalog_Block_Product_View.

Now if you do


your IDE supporting code completion will give you a drop down of all the available methods, let’s say canEmailToFriend();


Now all you need to do is to replace  Mage_Catalog_Block_Product_View with $this like


And you’re done. All of this may look like “why do I need this”. What you need it a smart IDE, one that can figure out the context of $this by it self and call the methods accordingly. No IDE currently does that, if I’m not missing on something. For now I see no better solution to retrieve the object context of $this across all those Magento files.

If you need some help setting up Krumo with Magento you can read my other, somewhat detailed article on

Hope this was useful for you.

You made it all the way down here so you must have enjoyed this post! You may also like:

Custom shipping method in Magento Stjepan Udovicic
, | 44

Custom shipping method in Magento

Getting started with Magento ORM, setting up the model Branko Ajzele
Branko Ajzele, | 11

Getting started with Magento ORM, setting up the model

Making use of Magento getSingleton method Branko Ajzele
Branko Ajzele, | 19

Making use of Magento getSingleton method


  1. maybe you should just set your include path / external folders for the project – in IDE’s like Eclipse this work every time and the IDE is then able to Reflect en give the correct code hints, else how is the IDE suppose to magically know where all the class files reside.

  2. Another options:

    * To find the class of an object use get_class($this)

    * To get publicly available properties of Varien_Object use print_r($this->debug())

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.

Tell us about your project

Drop us a line. We'd love to know more about your project.