Tsundoku
Tsundoku

Reputation: 9408

Call one function from another using codeigniter

I'm using CodeIgniter and I came across an interesting problem. 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:

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?

Upvotes: 1

Views: 3548

Answers (2)

Shawn C
Shawn C

Reputation: 126

Helper's are just functions so instead of calling them like a class with $this-> you just call them like a normal php function. 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']);

Upvotes: 0

Damien Pirsy
Damien Pirsy

Reputation: 25435

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

Upvotes: 3

Related Questions