One of the problems I’m facing when working under the hood of the Magento CMS is determining the context of $this. If you are to do any advanced stuff with your template, besides layoyout 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 do var_dump($this) your browser will return empty page after a short period of delay. 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)
Personally, I like using print_r() and var_dump(), mostly because so far I had no positive experience using fancy xdebug 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 http://www.php.net/manual/en/ini.core.php#ini.memory-limit to see how and what exactly.
Google search gave me some useful results trying to solve my problems. I found Krumo at http://krumo.sourceforge.net/. 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 your 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).
In order to use the Krumo at first place, we need to set it up to be used with Magento. After you download the Krumo, unarchive and copy it to some folder inside Magento root folder. I created /developer folder and copied all Krumo files to it.
Next you open the krumo.ini file and set the full path to Krumo folder (see the following image).
After setting up krumo.ini all you need to do is to include class.krumo.php into any file you wish to use it (see the following image).
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
Mage_Catalog_Block_Product_View::
your’re IDE supporting code completion will give you a drop down of all the available methods (see the following image), let’s say canEmailToFriend();
Mage_Catalog_Block_Product_View::canEmailToFriend();
Now all you need to do is to replace Mage_Catalog_Block_Product_View with $this like
$this->canEmailToFriend();
And your 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.
Krumo debugger tool has few other neat tricks in its sleeves. It can retrieve a list of all the available classes, constants and so on (see the following image).
For now I see no better solution to retrieve the object context of $this across all those Magento files. Hope this was helpful for you.
7 Responses to Debugging Magento using Krumo
Finding out Magento object context | Inchoo
September 10th, 2008 at 3:01 pm
[...] you need some help setting up Krumo with Magento you can read my other, somewhat detailed article on [...]
Gustavo
April 18th, 2009 at 10:52 pm
Very good.
I`m new in PHP.
I need to learn Magento because there are no frameworks for e-commerce in Java. Unfortunately.
I`m a java programmer and I see that Java is very, very better, at least for debug.
I am try to debug magento with Zend, but it crashes Apache. At least in my initial experience.
Thanks for your explanation.
Gustavo(Brazil)
Ps: And Forgive my poor English, please.
branko
April 19th, 2009 at 12:48 am
@Gustavo
Java, as well as C# (which I know a bit :)) are THE kick ass programming languages. When it comes to debugging requirements PHP cannot measure with those two. Main difference (in my opinion) is that PHP is not strongly typed, therefore debuggers broke and broke and broke…. God knows I tried XDebug and bunch of other debugging solutions… NONE of them will work on complex systems like Magento. I’ve read tens of so called Debugging HowTO’s, but they all seem to be based on “Here’s my index.php and my sample.php file. No let me include one in another and run the debugging.” Yea, good luck with that approach. Anyhow, I ended using my custom class that I extended from Exception class (PHP built one) and ended using something like “< ?php new acl($someVariable, "Some description, if any") ?>” where “acl” is the name of my custom class. Inside the class I made it to to various things, one of which is output to FireBug (FirePHP). Why inherit from Exception? -This enabled me to get name and line of file where I called it. Hope this helps.
Marco
April 28th, 2009 at 4:28 am
Hi Gustavo,
by saying there is no Java framework for ecommerce you are wrong. Check out apache ofbiz: http://ofbiz.apache.org/
but I think magento is the mightier one.
best regards, Marco
Sean
May 20th, 2009 at 7:22 pm
I like Java, too. Every time I try debugging something in PHP I like Java a little more
But Java web development, that’s a whole other story…
Michael
May 23rd, 2009 at 10:33 am
Why don’t you simply use ?
There is also an option in System -> Configuration -> Developer -> Debug -> Template Path Hints and …… -> Add Block Names To Hints
Just make sure to select a website or a store view in the Configuration Scope dropdown in left column, as these options are not displayed on “Default Config” level.
branko
May 23rd, 2009 at 10:37 am
People, people, people… I’m closing the comments for this post. I wrote it when I was first starting out with Magento. I use completely diferent and far more better debugging mechanisms for Magento.
I really appreciate the feedback, but this post is old stuff.