我是PHP和Magento的新手,我想弄清楚以下两行之间的区别:
$helper = Mage::helper('catalog/category');
$helper = $this->helper('catalog/category');
我在模板文件中看到了类似的代码,但是何时以及为什么我会使用一个而不是另一个?
第一行$helper = Mage::helper('catalog/category');
是将对象分配给助手.
第二行$helper = $this->helper('catalog/categry');
是将对象的属性赋值给变量 - 但只能在对象中使用它,因为它使用$this->
语法.
在对象内部通过$this->
外部引用它的属性,通过变量名称引用它,然后是属性$someVar->
.
另外需要注意的是,你的第一个语句是(正如Eric正确指出的那样),第一个语句可以是对静态方法的调用(这是一种在不创建对象实例的情况下运行对象函数的可爱方式 - 通常不起作用).
通常,您必须先创建一个对象,然后才能使用它:
class something
{
public $someProperty="Castle";
public static $my_static = 'foo';
}
echo $this->someProperty; // Error. Non-object. Using '$this->' outside of scope.
echo something::$someProperty; // Error. Non Static.
echo something::$my_static; // Works! Because the property is define as static.
$someVar = new something();
echo $someVar->someProperty; // Output: Castle
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
583 次 |
最近记录: |