Use of $installer v $this in install scripts

The answer is much simpler. In 2007 (and I believe till 2009 when PhpStorm started rocking) no one IDE allowed to provide inline phpdoc for $this. But core developers wanted to have autocompletion in IDE. That's why they used these 2 lines:

$installer = $this;
/* @var $installer <appropriate class> */

Some modules have own setup class and it should have been used in inline phpdoc. But because setup/upgrade script were (and are) always created via "copy/paste some existing one and change" you can find maybe examples when module has own setup class (or uses eav setup model Mage_Eav_Model_Entity_Setup) but Mage_Catalog_Model_Resource_Setup is used in inline phpdoc in upgrade script.


The oldest version I have is 1.0. Even then $installer = $this; existed. Even in the files named upgrade-0.x.y-0.z.t this line exists.

In my opinion, when they started (I mean version 0.1 or something like that) they had something like $installer = new Something() and they decided to change the logic.
I assume this because of the <class> tag in the config.xml of some modules (Mage_Catalog for example). Versions before 1.6:

<setup>
    <module>Mage_Catalog</module>
    <class>Mage_Catalog_Model_Resource_Eav_Mysql4_Setup</class>
</setup> 

Or in versions 1.6+ :

<setup>
    <module>Mage_Catalog</module>
    <class>Mage_Catalog_Model_Resource_Setup</class>
</setup>

I usually use $this instead of $installer and I didn't have any issue (if that maters in any way).


This is a convention of unknown and possibly dubious logic present since the earliest public beta from 2007 (Preview B1 0.6.12383; login required).

It is used as a convention to ensure that the class which is executing setup code is consistently aliased in setup scripts. For example, while both the Enterprise_GiftWrapping and Enterprise_Rma modules have their own setup classes, they each alias $installer to an instance of Mage_Catalog_Model_Resource_Setup when adding attributes to the product entity, e.g.:

app/code/core/Enterprise/GiftWrapping/sql/enterprise_giftwrapping_setup/install-1.11.0.0.php

<?php

$installer = $this;
/* @var $installer Enterprise_GiftWrapping_Model_Resource_Setup */
//... miscellaneous Enterprise_GiftWrapping setup logic

$installer = Mage::getResourceModel('catalog/setup', 'catalog_setup');
//... miscellaneous product entity attribute manipulation