[英]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.