我有一个具有下一个功能的控制器:
class controller {
function __construct(){
}
function myfunction(){
//here is my variable
$variable="hello"
}
function myotherfunction(){
//in this function I need to get the value $variable
$variable2=$variable
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢你的回答.如何将函数的变量传递给codeigniter控制器中的其他函数?
或者您可以将$ variable设置为您的类中的属性;
class controller extends CI_Controller {
public $variable = 'hola';
function __construct(){
}
public function myfunction(){
// echo out preset var
echo $this->variable;
// run other function
$this->myotherfunction();
echo $this->variable;
}
// if this function is called internally only change it to private, not public
// so it could be private function myotherfunction()
public function myotherfunction(){
// change value of var
$this->variable = 'adios';
}
}
Run Code Online (Sandbox Code Playgroud)
这种方式变量可用于控制器类中的所有函数/方法.认为OOP不是程序性的.