Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Lot of time, I heard that installer script, upgrade script, and data script files are extending Mage_Core_Model_Resource_Setup (default case). When we make a look on any of these file, there is no hint for this. For example, an installer script somewhat look like this.

#FILE : app/code/<any_code_pool>/<AnyNameSpace>/<AnyModuleName>/sql/<any_script_setup>/install-x.x.x.php
$installer =  $this;

//some stuffs here


As you can see, the file does not specify any class or not extending any class explicitly. And then also several tutorial says that $this is an instance of resource setup class. How it is possible?

Can you guys give some hints about this ?

share|improve this question
up vote 3 down vote accepted

It all starts in Mage_Core_Model_App::_initModules. Actually it starts higher in the stack but you are interested on this.
In this method there is this line:


And the method applyAllUpdates looks like this:

static public function applyAllUpdates()
    self::$_hadUpdates = false;

    $resources = Mage::getConfig()->getNode('global/resources')->children();
    $afterApplyUpdates = array();
    foreach ($resources as $resName => $resource) {
        if (!$resource->setup) {
        $className = __CLASS__;
        if (isset($resource->setup->class)) {
            $className = $resource->setup->getClassName();
        $setupClass = new $className($resName);
        if ($setupClass->getCallAfterApplyAllUpdates()) {
            $afterApplyUpdates[] = $setupClass;

    foreach ($afterApplyUpdates as $setupClass) {

    self::$_schemaUpdatesChecked = true;
    return true;

The interesting part is this:

        $className = __CLASS__;
        if (isset($resource->setup->class)) {
            $className = $resource->setup->getClassName();
        $setupClass = new $className($resName);

So if you specify a specific class for the upgrade scripts that will be instantiated, otherwise __CLASS__ (which is Mage_Core_Model_Resource_Setup) will be used.

And if you look for the method applyUpdates that is called in the code above you will end up after digging in _modifyResourceDb method in the same class where this code is what you are looking for

case 'php':
    $conn   = $this->getConnection();
    $result = include $fileName;

This means that the upgrade script is included in class you are using, hence you can access $this.

Side note: In a similar way you can access $this in template files. They are included in rendering method for blocks.

share|improve this answer
Oh i never knew you could make a .sql install script :) – David Manners Aug 1 '14 at 6:51
@DavidManners I don't think someone ever did. And since ce-1.6, when the install scripts changed to use DDL table definitinos, this seams kind of a bad practice. In my opinion the support for sql files should be removed. – Marius Aug 1 '14 at 6:53
We had this discussion yesterday if it is there someone will try ;) – David Manners Aug 1 '14 at 6:54
Yes. I digged upto applyAllUpdates() and saw the class instantiation. But was thinking from where this method get called. Your answer gave me the idea where it calls from. My doubt cleared. Thanks for this outstanding answer – Rajeev K Tomy Aug 1 '14 at 6:56
@DavidManners. Try it and let us know the result. :) – Marius Aug 1 '14 at 7:09

Magento creates a new class and uses function include to insert content of the install script. Similar to .phtml files that also use $this variable and it's the block object.

See Mage_Core_Model_Resource_Setup and method _modifyResourceDb

share|improve this answer
can you please elaborate your answer please? Where it includes this class? – Rajeev K Tomy Aug 1 '14 at 6:39

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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