You are here: Blog » Code Snippets » Magento Instantiate Block Class from Anywhere

Magento Instantiate Block Class from Anywhere

magento-logo

Let’s say you are working within a specific phtml template file, that is associated with the module name mymodule and class myclass. Suppose you observe the following class function being used:

Now let’s say you are working within another file somewhere and you want to access the awesome link. The problem is that you can no longer reference the desired class via the $this object.

What you need to do is instantiate the class as a variable, and then you can reference the member functions via that variable, the same way you would reference the $this variable in the original phtml file.

Here’s how to do that:

 

This entry was posted in Code Snippets by .

Author: justin saad

Justin Saad is first and foremost a husband and a father of four. He is also a web developer and designer who has been making websites for over twenty years. His company is called The Motech Network and his website is ClevelandWebDeveloper.com. In addition to building websites, Justin makes WordPress plugins and likes writing tutorials to help others learn about various aspects of web development.

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=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">