>Controller:
function act() {
//some code for connection
$input = (response from client);
return $input;
}
这是第一次调用该行为以连接到客户端。在这里,我将获得带有连接的输入变量。
function a() {
$a = $this->act();
}
如何在不再次连接的情况下获取此函数中的$input
?
function b() {
}
我试过把它放在会话闪存数据中,但它不起作用。
你不能。
为了获得该变量,您需要将其放在函数本身之外。
class MyController extends CI_Controller
{
private $variable;
private function act()
{
$input = (response from client)
return $input
}
private function a()
{
$this->variable = $this->act();
}
}
这样做将使您能够从类中的任何位置访问变量。
希望这有帮助。
在你的
class
定义一个变量
in controller class below function is written.
Class myclass {
public $_customvariable;
function act(){
//some code for connection
$this->_customvariable= $input = (response from client);
return $input;
}
function a() {
$a = $this->act();
}
function b(){
echo $this->_customvariable;//contains the $input value
}
}
class fooBar {
private $connection;
public function __construct() {
$this->act();
}
public function act(){
//some code for connection
$this->connection = (response from client);
}
public function a() {
doSomething($this->connection);
}
public function b() {
doSomething($this->connection);
}
}
您可以在方法或函数中使用静态变量,响应在函数中"缓存"
function act(){
static $input;
if (empty($input))
{
//some code for connection
$input = (response from client);
}
return $input;
}