【问题标题】:How to call one method from another method from the same controller in Kohana 3如何从 Kohana 3 中同一控制器的另一个方法调用一个方法
【发布时间】:2015-10-22 21:47:35
【问题描述】:

我有两种方法,我想在第一种方法中调用另一种方法。它们在同一个控制器中。我尝试过这种方式,但出现错误:

调用未定义的方法Controller_User::getUser()

我的控制器是这样的:

<?php defined('SYSPATH') or die('No direct script access.');

class Controller_User extends Controller {

   public function action_index (){
       $id = $this->request->param('id');
       $user = self::getUser($id);
  }

 public function action_getUser ($id){
      //some code here
  }


}

【问题讨论】:

  • 该方法被命名为action_getUser,而不仅仅是getUser$this-&gt;action_getUser($id) 也可以工作(如果您不希望这样做,请将方法声明为 static
  • 谢谢,现在很好。

标签: methods controller kohana


【解决方案1】:

这两个函数都在同一个类中,所以使用 $this-> 来调用同一个类中的其他方法,就像评论用户 $this->action_getUser($id) 中提到的 kingkero 一样

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-26
    • 1970-01-01
    • 2011-09-23
    • 1970-01-01
    • 2019-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多