Use of $installer v $this in install scripts
-
16-10-2019 - |
Question
Ok, using install scripts there seems to be a weird convention to use the following:
$installer = $this;
I really don't understand this as it is completely redundant.
Why not just use $this->
throughout the script?
Any ideas on why this convention exists?
Solution
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.
OTHER TIPS
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
One thing I like about using $installer
that I really want to add is that it makes it easy to replace it with something else or to run it outside of the class scope.
1. Replacing it:
$installer = $this;
/* @var $installer Mage_Core_Model_Resource_Setup */
// Do basic stuff
$installer = Mage::getSingleton('eav/entity_setup', 'eav_setup');
/* @var $installer Mage_Eav_Model_Entity_Setup */
/// Do stuff with attributes
2. Outside scope:
$ php -a php > require_once 'app/Mage.php'; php > Mage::init(); php > require 'app/code/local/Vendor/Module/data/vendormodule_setup/data-upgrade-0.1.1-0.1.2.php'; Upgrade worked! Debug data dump: array(4) { 'id' => int(123) 'foo' => string(3) "bar" }
Of course with data-upgrade-0.1.1-0.1.2.php
having:
//$installer = $this;
$installer = Mage::getResourceSingleton('core/setup', 'vendormodule_setup');
/* @var $installer Mage_Core_Model_Resource_Setup */
// Do lots of stuff ...
echo "Upgrade worked!\n";
echo "Debug data dump: ";
var_dump($debug);
So this prevents Fatal error: Using $this when not in object context
I would guess it is from earlier days (<v.1.1). But to be honest, I have no idea. I think it is a bit better readable...
We have always done it like this
You know ;-)