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

时间:2011-06-21 22:55:40

标签: codeigniter php

我正在使用CodeIgniter,我遇到了一个有趣的问题。我需要使用另一个函数中的变量。我计划通过简单地在框架中声明全局变量(我无法做到)来做到这一点。所以我尝试从另一个函数中调用一个函数(这都发生在控制器中)。由于显然无法完成,我使用常用​​函数创建了一个帮助文件然后只是尝试加载它但是我收到了这个错误:

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

帮助文件位于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;
            }
        }

我正试着这样做:

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

我可以在这里找到什么?

2 个答案:

答案 0 :(得分:3)

试试这个:

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

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

帮助程序(如果正确完成)只是一组函数,而不是类,因此您可以将其称为常规函数调用。

你也应该在你的助手中这样做:

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

避免多次加载时出现'重新声明功能'错误

答案 1 :(得分:0)

Helper只是函数,所以不要把它们称为类似$ this->的类。你只需称它们为普通的php函数。所以改变这个

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

到这个

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