辅助功能中的功能中的Codeigniter使用功能

问题描述 投票:0回答:2

我想在codeigniter的帮助器中的另一个函数中使用一个函数。

所以通常我可以这样做

 function first($val){
    $ci= & get_instance();

   //do something
    return "hello";
}

function second($val){
      $ci= & get_instance();
    $this->first($val);

// try to do this failed also
// $ci->first($val);

}

我做错了什么?

php codeigniter
2个回答
0
投票

在类似的另一个功能中使用第一个功能。

 function first($val){
    $ci= & get_instance();

   //do something
    return "hello";
}

function second($val){
    $ci= & get_instance();
    first($val);
}

0
投票

与CodeIgniter中的大多数其他系统不同,辅助程序不是以面向对象的格式编写的。它们是简单的程序功能。每个助手功能执行一项特定任务,而不依赖于其他功能。 (Reference

因此,您应该在没有$this的情况下使用>

function second($val){
    $ci= & get_instance();
    first($val);
}

但是,如果您在类内部定义这些函数(在控制器,模型等中)。您应该使用$this

class Control extends CI_Controller{
    function first($val){
         $ci= & get_instance();

        //do something
        return "hello";
    }

   function second($val){
       $ci= & get_instance();
       $this->first($val);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.