6

So point is simple. If I have a template that is defined like this

<block type="custom/block" name="custom.block" template="custom/template.phtml" />

Then $this that I am going to use in the template custom/template.phtml will stand for the block class Namespace_Modulename_Block_Custom_Block. How it is making possible in Magento.

Recently I have tried to do the same trick in a core php work. But when I try to do

$this =  new someClass();

It says, you cannot alter $this variable like that.

This doubt is with me from the start time. I really need a nice explanation(using relevant codes) for this.

So can you enlighten me with your knowledge on this?

5

since the template phtml file is loaded in the Namespace_Modulename_Block_Custom_Block class $this is an instance for the class you are in at that moment. It's reserved by PHP and cannot be overwritten.

Since your block class extends Mage_Core_Block_Template, the class where the phtml file is included in, $this is an instance of your class

class Mage_Core_Block_Template
{
   [...]
   public function fetchView($fileName)
   {
        [...] 
        try {
            $includeFilePath = realpath($this->_viewDir . DS . $fileName);
            if (strpos($includeFilePath, realpath($this->_viewDir)) === 0 || $this->_getAllowSymlinks()) {
                include $includeFilePath; // HERE YOUR PHTML FILE IS LOADED
            } else {
                Mage::log('Not valid template file:'.$fileName, Zend_Log::CRIT, null, null, true);
            }

        }
   }
   [...]
}
1
  • Thanks for your quick response ! My doubt cleared. More power to you :-) – Rajeev K Tomy Oct 14 '14 at 7:20
6

First see the php basics.

Note: $this is a special variable that can't be assigned.

Now to explain why $this works in templates.
When inside a template you are actually inside a block class, so $this refers to the current object instance.
Here is why.
To render a block you have to call $block->toHtml() This is defined in Mage_Core_Block_Abstract class. All blocks extend that class.
Then this method calls _toHtml.
For blocks that use templates _toHtml looks like this:

protected function _toHtml()
{
    if (!$this->getTemplate()) {
        return '';
    }
    $html = $this->renderView();
    return $html;
} 

Going deeper you end up in the fetchView method.
Somewhere in that method the template file is included in the method.
This way you can access $this because you are in the class itself.

It's something similar to doing this.

You have file a.php that contains

<?php 
echo $a;

This would give you a warning because $a is not defined but if you use it like this:

$a = 4;
include 'a.php';

you will get a valid output because $a is recognized in the included file.

1
  • Yes I know $this is a reserved variable. I just shown that code in order to show its importance. So that is the trick. The template file included inside the block class. That is why $this do that magic !. I cannot accept all of your answers. Let us go for @SanderMangel, since he gave us "first" detailed answer. :-) – Rajeev K Tomy Oct 14 '14 at 7:19
5

The variable $this is simply a reference to the current object, nothing more and nothing less. In your case it is simply the block defined in the type.

It is common practice when dealing with object oriented.

The keyword $this is used to refer to properties or methods within the class itself. The $this keyword is reserved in PHP, so it cannot be used as a variable or property name.

So this means that the $this is reserved in php and so you cannot set it a something else as per your example.

The pseudo-variable $this is available when a method is called from within an object context. $this is a reference to the calling object (usually the object to which the method belongs, but possibly another object, if the method is called statically from the context of a secondary object).

1
  • 1
    Yes I know $this is a reserved variable. I just shown that code in order to show its importance. So that is the trick. The template file included inside the block class. That is why $this do that magic !. I cannot accept all of your answers. Let us go for @SanderMangel, since he gave us first detailed answer. :-) – Rajeev K Tomy Oct 14 '14 at 7:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.