0

I have created a custom block and when I try to access a method from my block I get:

exception 'Exception' with message 'Notice: Undefined variable: this­ in /Applications/XAMPP/xamppfiles/htdocs/mag19/app/design/frontend/vish/default/template/Vish/sizecheck/sizecheck.phtml on line 2' in /Applications/XAMPP/xamppfiles/htdocs/mag19/app/code/core/Mage/Core/functions.php:232

My sizecheck.phtml:

    <?php
$products = $this­->igetText();
?>

<div id="product_list">
  <?php echo($products);?>
</div>

The block:

    <?php
class Vish_SizeCheck_Block_Size extends Mage_Core_Block_Template {
      public function igetText()
    {
        return "I am inside `" . get_class(). "` block and it is my first custom block ever !";
    }
}

config.xml:

    <?xml version="1.0"?>
<config>
  <modules>
    <Vish_SizeCheck>
      <version>0.0.1</version>
    </Vish_SizeCheck>
  </modules>
  <frontend>
       <routers>
          <sizecheck>
              <use>standard</use>
              <args>
                 <module>Vish_SizeCheck</module>
                 <frontName>sizechecker</frontName>
              </args>
           </sizecheck>
       </routers>
       <layout>
            <updates>
                <vish_sizecheck>
                    <file>sizecheck.xml</file>
                </vish_sizecheck>                
            </updates>
        </layout>
    </frontend>
  <global>
          <blocks>
      <vish_sizecheck>
        <class>Vish_SizeCheck_Block</class>
      </vish_sizecheck>
    </blocks>
    <models>
       <vish_sizecheck>
            <class>Vish_SizeCheck_Model</class>
        </vish_sizecheck>
    </models>
    <helpers>
      <sizecheck>
        <class>Vish_SizeCheck_Helper</class>
      </sizecheck>
    </helpers>
  </global>
</config>

my sizecheck.xml:

    <?xml version="1.0"?>
<layout version="0.1.0">
    <sizecheck_index_index>
        <reference name="root">
            <action method="setTemplate"><template>page/1column.phtml</template></action>
        </reference>
        <reference name="content">
            <block type="core/template" name="sizecheck.form" as="sizecheck_form" template="Vish/sizecheck/form.phtml"/>
        </reference>
    </sizecheck_index_index>
</layout>

My block declaration in CMS Page:

{{block type="vish_sizecheck/size" name="sizecheck" template="Librex/sizecheck/sizecheck.phtml"}}

I can see the class is correctly inherited by:

echo get_class($this);
  • Show your sizecheck.xml. – Sohel Rana Jul 1 at 15:48
  • @SohelRana that is for the route part only but will add now – Vaishal Patel Jul 1 at 15:55
  • Is it is comes in igetText()? did you try die('here') in your funciton ? – Ravi Soni Jul 1 at 17:30
0

The solution was rather staring me in the face. Looking at the error,

'Notice: Undefined variable: this­ in...

It was not instantiating the variable $this, even though the line was set as:

 <?php
  $products = $this­->igetText();
 ?>

Debugging this line further it revealed there was a hidden character:

The hidden character

So, deleting this line and retyping the line got everything working.

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.