【问题标题】:Joomla call internal controller functionJoomla 调用内部控制器功能
【发布时间】:2013-12-10 11:36:51
【问题描述】:

如何在同一个控制器文件中调用其他方法/函数?

例如:controller.php

class MyController extends JControllerLegacy
{
   function test() {
       $var = otherfunction();
       echo $var;
   }
   function othermethod() {
       return 'something';
   }
}

我在浏览器中收到错误消息。 致命错误:“调用未定义函数 othermethod()”。

我很乐意得到帮助...抱歉我的英语不好(;

【问题讨论】:

    标签: php joomla controller components


    【解决方案1】:

    为了在同一个类中调用方法,您可以使用this。而要调用函数,您需要包含具有函数定义的文件。

    class MyController extends JControllerLegacy
    {
       function test() {
           //include_once file_has_otherfunction_definition.php
           //$var = otherfunction();
           $var = $this->othermethod();
           echo $var;
       }
       function othermethod() {
           return 'something';
       }
    }
    

    【讨论】:

    • 非常感谢 Irfan :)
    【解决方案2】:

    你没有正确调用 joomla 控制器。

    语法如下: 类组件名称控制器控制器名称{ }

    ex:class meadowmartControllercort 扩展了 Jcontroller{ } 这里的deremmart 是组件名称,cort 是控制器。Controller 表示this 是控制器。

    【讨论】:

      【解决方案3】:

      在平面 php 编程中,您可以通过函数名称调用函数,例如:

      otherfunction();
      

      但如果编程面向对象(如您插入的代码),则必须使用 $this->functionName/ 来调用其他函数。所以 test() 函数必须变成这样:

      function test() {
         $var = **$this->otherfunction();**
         echo $var;
      

      } 有关面向对象 PHP 的更多信息,请参阅this

      【讨论】:

      • 非常感谢 mohsenkw... 好网站,合格用户,快速响应!我喜欢:)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-20
      • 2019-06-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多