Answers:
答案要简单得多。在2007年(我相信直到2009年PhpStorm开始摇摆不定),没有一个IDE允许为其提供内联phpdoc $this。但是核心开发人员希望在IDE中具有自动补全功能。这就是为什么他们使用这两行:
$installer = $this;
/* @var $installer <appropriate class> */
一些模块有自己的安装程序类,应该在嵌入式phpdoc中使用它。但是,由于安装/升级脚本总是(并且总是)通过“复制/粘贴一些现有脚本并进行更改”创建的,因此当模块具有自己的安装类(或使用eav安装模型Mage_Eav_Model_Entity_Setup)但Mage_Catalog_Model_Resource_Setup在升级中用于内联phpdoc时,您可能会找到示例脚本。
我拥有的最旧的版本是1.0。即使那样$installer = $this;存在。即使在名为upgrade-0.x.y-0.z.t该行的文件中也存在。
我认为,当他们开始(我的意思是版本0.1或类似的东西)时,他们有类似的东西$installer = new Something(),他们决定更改逻辑。
我之所以这样认为是因为某些模块(例如Mage_Catalog)中的<class>标签config.xml。1.6之前的版本:
<setup>
<module>Mage_Catalog</module>
<class>Mage_Catalog_Model_Resource_Eav_Mysql4_Setup</class>
</setup>
或在1.6+版本中:
<setup>
<module>Mage_Catalog</module>
<class>Mage_Catalog_Model_Resource_Setup</class>
</setup>
我通常使用$this代替,$installer并且我没有任何问题(如果有任何问题)。
自从2007年最早的公开测试版以来,这是一个未知且可能存在疑问的逻辑惯例(预览版B1 0.6.12383;需要登录)。
它用作约定,以确保正在执行安装代码的类在安装脚本中始终具有别名。例如,虽然Enterprise_GiftWrapping和Enterprise_Rma模块都有各自的设置类,但它们各自将别名添加到产品实体$installer的实例Mage_Catalog_Model_Resource_Setup,例如:
应用程序/代码/核心/企业/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
$installer我真正想添加的关于使用的一件事是,它可以轻松地用其他东西替换它或在类范围之外运行它。
1.更换:
$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.范围:
$ php -a
php> require_once'app / Mage.php';
php>法师:: init();
php>需要'app / code / local / Vendor / Module / data / vendormodule_setup / data-upgrade-0.1.1-0.1.2.php';
升级有效!
调试数据转储:array(4){
'id'=>
整数(123)
'foo'=>
string(3)“ bar”
}
当然data-upgrade-0.1.1-0.1.2.php有:
//$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);
所以这可以防止 Fatal error: Using $this when not in object context
我想这是从早期开始(<v.1.1)。但是说实话,我不知道。我认为它的可读性更好...
我们一直这样做
你懂 ;-)