简体   繁体   English

使用codeigniter从另一个函数调用一个函数

[英]Call one function from another using codeigniter

I'm using CodeIgniter and I came across an interesting problem. 我正在使用CodeIgniter,遇到一个有趣的问题。 I need to use the variables from one function on another. 我需要在另一个函数上使用变量。 I was planning to do this by simply declaring global variables (which I wasn't able to) do in the framework. 我打算通过简单地声明框架中的全局变量(我无法做到)来做到这一点。 So I tried calling one function from within another (this is all happening in the controller). 因此,我尝试从另一个函数中调用一个函数(这全部发生在控制器中)。 Since apparently this can't be done I made a helper file with the common function and then just tried to load it but I get this error: 由于显然无法做到这一点,所以我用通用函数制作了一个辅助文件,然后尝试加载它,但出现此错误:

Fatal error: Call to undefined method ReporteNominas::getValues()

the helper file is inside the helpers folder and it contains this: helper文件位于helpers文件夹中,包含以下内容:

function getValues($getThem, $tpar, $vpiso, $tcomi, $tgas, $ttotal){
            $totalPares = $tpar;
            $ventasPiso = $vpiso;
            $totalComisiones = $tcomi;
            $totalGastos = $tgas;
            $totalTotal = $ttotal;
            if($getThem){
                return $totalPares . "," . $ventasPiso . "," . $totalComisiones . "," . $totalGastos . "," . $totalTotal;
            }
        }

and I'm trying to call it doing this: 我试图称其为:

$this->load->helper('helper_common_functions_helper');
                $this->getValues(false, $query['cant'], $query['sum'], $query['com'], $query['gas'], $query['tot']);

what could I be missing here? 我在这里会想念什么?

Try this: 尝试这个:

$this->load->helper('helper_common_functions_helper');

getValues(false, $query['cant'], $query['sum'], $query['com'], $query['gas'], $query['tot']);

A helper (if done properly) is just a group of functions, NOT a class, so you can call it as a regular function call. 一个帮助器(如果正确完成的话)只是一组函数,而不是一个类,因此您可以将其作为常规函数调用来调用。

You should also do this, in your helper: 您还应该在您的助手中执行此操作:

if ( ! function_exists('get_values'))
{
   function getValues($getThem, $tpar, $vpiso, $tcomi, $tgas, $ttotal)
   {
     //rest of code
   }
}

To avoid a 'redeclare function' error when loaded more than once 为避免多次加载时出现“重新声明函数”错误

Helper's are just functions so instead of calling them like a class with $this-> you just call them like a normal php function. 助手只是函数,因此与其像$ this->这样的类来调用它们,不如像普通的php函数那样调用它们。 So change this 所以改变这个

$this->getValues(false, $query['cant'], $query['sum'], $query['com'], $query['gas'],$query['tot']);

to this 对此

 getValues(false, $query['cant'], $query['sum'], $query['com'], $query['gas'],$query['tot']);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过使用会话将数据从一个功能传递到Codeigniter中的另一个功能? - How to pass data from one function to another in codeigniter by using session? 在同一模型类的Codeigniter中调用一个函数到另一个函数不起作用 - call one function to another function in the same model class of codeigniter is not working 如何从其他Codeigniter调用另一个Codeigniter类/函数 - How to call another Codeigniter Class/function from other Codeigniter 如何在codeigniter中调用另一个控制器中的一个控制器功能 - How to call one controller function in another controller in codeigniter 如何在另一个视图中调用一个视图的jQuery函数 - how to call a jquery function of one view in another view codeigniter 从codeigniter中的控制器功能调用另一个控制器功能 - call another controller function from controller function in codeigniter 如何将数组值从一个函数传递到另一个函数,并使用Codeigniter将其发送以查看页面? - How to pass the array value from one function to another function and send it to view the page using Codeigniter? 如何将信息从一个模型调用到另一个Codeigniter - How to call information from one model to another Codeigniter 可以在 CodeIgniter 的 model 中调用另一个 model 的方法吗? - Can one call a method from another model in a model in CodeIgniter? 是否可以在Codeigniter中从另一个控制器调用方法? - Is it possible to call methods from one controller on another in Codeigniter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM