【问题标题】:how to call a static method in a custom magento class如何在自定义 magento 类中调用静态方法
【发布时间】:2010-03-03 09:58:28
【问题描述】:

我在带有静态函数的自定义 Magento 模型中有一个自定义模型:

class ABC_Module_Model_ClassName
{
    static public function send ( $something)
    {
         // do something static
    }
}

现在我这样调用函数:

ABC_Module_Model_ClassName::send($something); // works and is nothing wrong with it

出于一致性目的,我想知道 Mage 是否有内部调用静态方法的方式,如下所示:

Mage::getModel('abc/module_className')::send($something); // this is wrong
// or 
Mage::getModel('abc/module_className', send($something)); // with a callback or something

【问题讨论】:

    标签: php magento static


    【解决方案1】:

    鉴于像Mage::getModel() 这样的任何方法实际上都会返回该类的一个实例,因此您将动态调用它而不是静态调用它。例如,您将使用 $module->staticMethod(); 而不是 Module::staticMethod().

    所以你最好的就是

    • 要么将静态方法作为常规函数,以便全局可用,
    • 将所有静态方法放在一个类中,并将该类命名为 Common 这样您就不必键入很长的名称,
    • 或者只是按照您在问题中所做的方式静态调用它,例如Module::method()

    最后,静态调用方法的唯一方法是通过Class::method()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-05
      • 2021-04-09
      • 1970-01-01
      • 2012-06-25
      • 1970-01-01
      • 2011-10-28
      • 2011-01-13
      相关资源
      最近更新 更多